gpt4 book ai didi

javascript - 如何在javascript中动态合并数组?

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

var series = 
[
[{
Name: "A",
Type: "SC"
}, {
Name: "B",
Type: "SC"
}, {
Name: "C",
Type: "SC"
}, {
Name: "D",
Type: "SC"
}],
{
ColumnName: "Target",
Type: "Line"
},
{
ColumnName: "bar",
Type: "Line"
}
];

我想按如下方式合并数组

var series = [ { Name: "A", Type: "SC" }, { Name: "B", Type: "SC" }, { Name: "C", Type: "SC" }, { Name: "D", Type: "SC" }, { ColumnName: "Target", Type: "Line" }, { ColumnName: "bar", Type: "Line" } ];

非常感谢任何帮助。

最佳答案

JS 有 Array.flat()可以展平数组的方法:

const series = [[{"Name":"A","Type":"SC"},{"Name":"B","Type":"SC"},{"Name":"C","Type":"SC"},{"Name":"D","Type":"SC"}],{"ColumnName":"Target","Type":"Line"},{"ColumnName":"bar","Type":"Line"}];

const flattened = series.flat();

console.log(flattened);

旧答案:

Spread数组变成Array.concat() :

const series = [[{"Name":"A","Type":"SC"},{"Name":"B","Type":"SC"},{"Name":"C","Type":"SC"},{"Name":"D","Type":"SC"}],{"ColumnName":"Target","Type":"Line"},{"ColumnName":"bar","Type":"Line"}];

const flattened = [].concat(...series);

console.log(flattened);

关于javascript - 如何在javascript中动态合并数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49641195/

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