3
ここで何が起こっているのか理解してもらえますか?array.pushは関数ではありません - reduceで作業する場合
let firstArray = [];
firstArray.push(1);
firstArray.push(1);
firstArray.push(1);
console.log("firstArray", firstArray); // result [ 1, 1, 1 ] - as expected.
let secondArray = [1, 2, 3].reduce((acc, item) => {
console.log("acc", acc);
console.log("typeof acc", typeof acc);
// on first passing, the accumulator (acc) is Array[] == object.
// on the second passing the acc == number.
// but why?
/// i expect to get [1,1,1] as my secondArray.
return acc.push(1);
}, []);
console.log("secondArray", secondArray);
「acc.pushは関数ではありません」
そして、最初のaccumulator
をログに記録を検査するとプログラムがクラッシュは、我々がpushメソッドを持っていることを示している - それは本当の機能です。
'リターンacc.concat(項目);' – fubar
は 'プッシュ要素を返すpush'していますか?別の行を押してから、accを返してください。 – Carcigenicate