Skip to content
Advertisement

JavaScript: Looping through an array of objects and filtering specific properties/attributes

I’m trying to loop through an array and return just the addresses and the number of packages and then having the total number of packages in the array added up. Problem is, when I write this all in codepen.io, it tells me that each name attribute is having an unexpected identifier error. I know I’ll have to work on the loop itself but what is wrong with this? Here is what I’m seeing in Codepen.io and here is the link if you want to see what codepen is showing: https://codepen.io/epbutterfield/pen/NBxMQb?editors=0012

I know it’s super simple but I must have an extra identifier or am missing one….

JavaScript

Advertisement

Answer

Add quotes around your name values, because they’re string literals and not JS variables. Also use an extra variable, call it totalPackages, to sum up all of the packages in your loop.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement