Array.Push does is not pushing the data

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);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you. Noted - new user not aware of the details.

I’ve also been struggling with it. I can’t get either of your code blocks to work. As best I can tell the catch is this line:
acc.push(curr.fisrtName)
You are trying to use an array method on an object. Just to clarify acc is an object stored in your users array.
Hope that helps!

1 Like

arr.push is a function. How do we execute functions? :wink:

3 Likes

Thanks for pointing. This helps.

Thanks for the help. This helps