首页 > 解决方案 > 在数组内映射数组

问题描述

我正在尝试将所有项目映射到一个数组中:

[
  [
    { 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)}) 了,但这并没有改变

标签: javascript

解决方案


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


推荐阅读