javascript - 在数组内映射数组
问题描述
我正在尝试将所有项目映射到一个数组中:
[
[
{ id: 0, name: 'New Project', apiKey: '.'},
{ id: 1, name: 'New Project', apiKey: '.}
],[
{ id: 3, name: 'New Project', apiKey: '.'},
{ id: 4, name: 'New Project', apiKey: '.}
]
]
多合一数组(输出):
[
{ id: 0, name: 'New Project', apiKey: '.'},
{ id: 1, name: 'New Project', apiKey: '.},
{ id: 3, name: 'New Project', apiKey: '.'},
{ id: 4, name: 'New Project', apiKey: '.}
]
我试过var result = allUserProjects.map(user => {user.map(project => project)})
了,但这并没有改变
解决方案
const arr = [
[
{ id: 0, name: 'New Project', apiKey: '.'},
{ id: 1, name: 'New Project', apiKey: '.'}
],[
{ id: 3, name: 'New Project', apiKey: '.'},
{ id: 4, name: 'New Project', apiKey: '.'}
]
];
const output = arr.flat();
console.log(output)
您可以使用Array.flat()
:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
推荐阅读
- php - 替换除换行符以外的所有不可打印字符
- java - 如何解决java中的箭头按键错误
- python - 如何使用霍夫线检测来检测时钟指针
- android - 无法将方法调用“列”内联到本地最终乐趣中
() 在 Jetpack 组合中 - r - 使用 R 包解压缩包含多个文件的受密码保护的文件夹
- php - 获取数据后Facebook登录php hash_hmac()问题
- wordpress - 在 Easy digital Downloads 中显示特定类别的产品
- wordpress - Wordpress woocommerce 自动重新加载
- ansible - 遍历 yaml 数据值的数组
- flutter - 错误:对空值使用空检查运算符