gpt4 book ai didi

javascript - Push方法插入嵌套记录

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:44 24 4
gpt4 key购买 nike

我有两个数组对象,想要合并它们。但由于某种原因,它被插入到第一个数组的下一层

let first = [{name:'a'},{name:'b'}];
let first2 = [{name:'aa'},{name:'bb'}];
first.push(first2);

现在如果我打印第一个对象,它会给出以下输出

[{name: "a"},{name: "b"},[ {name: "aa"}, {name: "bb"}]]

而我需要这个输出[ {name: "a"}, {name: "b"}, {name: "aa"}, {name: "bb"}]

最佳答案

Spread first2 数组来推送项目而不是整个数组:

let first = [{name:'a'},{name:'b'}];
let first2 = [{name:'aa'},{name:'bb'}];
first.push(...first2);

console.log(first);

关于javascript - Push方法插入嵌套记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52901212/

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