gpt4 book ai didi

javascript - array.push 不是函数 - 使用 reduce 时

转载 作者:可可西里 更新时间:2023-11-01 01:37:47 24 4
gpt4 key购买 nike

<分区>

谁能帮我理解这是怎么回事?

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 is not a function in reduce

检查第一个记录的 accumulator 显示我们有 push 方法 - 这是一个真正的函数:

array.push not working with reduce

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com