gpt4 book ai didi

javascript - 将数组对象 block 合并到单个数组

转载 作者:行者123 更新时间:2023-11-30 07:20:13 25 4
gpt4 key购买 nike

我必须在 Angularjs 中将数组 block 合并为单个数组和对象数组。

我的输入数组将是这样的:

[
[
{
"title": "asd",
"description": "asd"
},
{
"title": "asz",
"description": "sd"
}
],
[
{
"title": "ws",
"description": "sd"
},
{
"title": "re",
"description": "sd"
}
],
[
{
"title": "32",
"description": "xxs"
},
{
"title": "xxc",
"description": "11"
}
]
]

上面的输入数组应该像下面的对象数组一样保存

[
{
"title": "asd",
"description": "asd"
},
{
"title": "asz",
"description": "sd"
},
{
"title": "ws",
"description": "sd"
},
{
"title": "re",
"description": "sd"
},
{
"title": "32",
"description": "xxs"
},
{
"title": "xxc",
"description": "11"
}
]

请建议我如何实现这一目标。

提前致谢

最佳答案

您可以使用 .reduce().concat() .

let data = [[{"title": "asd","description": "asd"},{"title": "asz","description": "sd"}],[{"title": "ws","description": "sd"},{"title": "re","description": "sd"}],[{"title": "32","description": "xxs"},{"title": "xxc","description": "11"}]];

let result = data.reduce((a, c) => a.concat(c), []);

console.log(result);

关于javascript - 将数组对象 block 合并到单个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50618091/

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