I'm learning the reduce method and I am trying to use it on this array of objects but i cant seem to figure it out here. the objective is to add all of the age elements together, I can do it with a for loop, but I cant seem to figure it out how to achieve the same with a reduce() method.
let array1 = [
{
"name": 'Neo',
"age":'28',
"fav Food": "Sea Food"
},
{
"name": 'Charlie',
"age":'20',
"fav Food": "Sushi"
},
{
"name": 'Benjamin',
"age":'21',
"fav Food": "Asian"
},
{
"name": 'Martha',
"age":'47',
"fav Food": "Italian"
}
]
I want to transform this for loop into a reduce method.
let sum=0;
for (let i = 0; i<array1.length;i++){
sum += parseInt(array1[i].age);
}
so far I have this but its not working, any help would be greatly appreciated.
const sum2 = array1.reduce( (accumulation, item) => {
return accumulation += (item = parseInt(array1[0].age));
},0 );