array.push is not a function – when working with reduce [duplicate]

The return value of Array#push is the new length of the array after the push. This means that in the second iteration acc is a number, which doesn’t have the push method.

The fix is simple – separate the push and return statements:

const secondArray = [1, 2, 3].reduce((acc, item) => {
    acc.push(1);

    return acc;
}, []);

console.log(secondArray);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)