Recently delving into JavaScript, I find myself a bit perplexed despite scouring through various answers and resources on Mozilla.org. My struggle lies in seamlessly using .map and .filter on straightforward arrays, but feeling a bit lost when it comes to arrays containing objects.
For instance, I need to calculate the total sum of all ages in a given array.
I understand that .map creates a new array, which is not what I need in this scenario, .filter is used to return elements based on a specific condition, and forEach doesn't return any values.
Although I am restricted from using a for loop, it seems like the most practical solution in this case. (The code snippet below is accurate and functional).
Are there any alternative methods to approach this issue?
const users=[
{firstName:"john",lastName:"Biden",age:26},
{firstName:"jimmy",lastName:"cob",age:75},
{firstName:"sam",lastName:"lewis",age:50},
{firstName:"Ronald",lastName:"Mathew",age:26},
];
var total=0
//console.log(users.length)
for(let i=0;i<users.length;i++){
//console.log(users[i].age)
total+=users[i].age
}
console.log(total)