Please can someone review and let me know what is wrong with acc.push[curr.fisrtName]. The value does not push the data in to the array for some reason. I have tried the same example with a different data set which works…
const users = [
{fisrtName:"Mike", lastName:"Doe", age:25},
{fisrtName:"John", lastName:"Doe", age:50},
{fisrtName:"Mary", lastName:"Doe", age:10}
];
const output1 = users.reduce(function (acc, curr) {
console.log('Current ' , curr)
console.log('Accumulate ' , JSON.stringify(acc))
if (curr.age < 40) {
acc.push[curr.fisrtName]
console.log('data pushed')
console.log(acc)
}
console.log('Accumulate Before return ' , JSON.stringify(acc))
return acc
},[])
console.log(output1)
---- Example Below Works—
const user1 = [
{ firstname: "Mohammad", lastname: "Noushad", age: 22 },
{ firstname: "Aniket", lastname: "Bhalla", age: 45 },
{ firstname: "Bidhi", lastname: "Chand", age: 21 },
{ firstname: "Saif", lastname: "Siddiqi", age: 67 },
];
const output2 = user1.reduce((acc, current) => {
if(current.age < 30){
acc.push(current.firstname);
}
return acc;
}, []);
console.log(output2);