gpt4 book ai didi

javascript - array.flat() 上的链函数不起作用

转载 作者:行者123 更新时间:2023-11-29 16:31:28 24 4
gpt4 key购买 nike

为什么 array.flat 上的链接函数没有按预期工作

const input = [[[{"type":"banana"},{"type":"orange"}]]];

console.log(input.flat(2).push({"type":"grapes"}));

我期望的是,它应该删除 2 数组包装器并将一个新项目推送到 input

但我只得到长度,这是为什么呢?

最佳答案

Array#push返回数组的新长度。

您可以将数组 ( Array#concat) 与新元素连接起来并获得新数组。

console.log(input.flat(2).concat({ type: "grapes" }));

关于javascript - array.flat() 上的链函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281823/

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