0

I'm trying to map all projects in one array:

[
  [
    { id: 0, name: 'New Project', apiKey: '.'},
    { id: 1, name: 'New Project', apiKey: '.}
  ],[
    { id: 3, name: 'New Project', apiKey: '.'},
    { id: 4, name: 'New Project', apiKey: '.}
  ]
]

All in one array (output):

[
 { id: 0, name: 'New Project', apiKey: '.'},
 { id: 1, name: 'New Project', apiKey: '.},
 { id: 3, name: 'New Project', apiKey: '.'},
 { id: 4, name: 'New Project', apiKey: '.}
]

I tried var result = allUserProjects.map(user => {user.map(project => project)}) but that changes nothing

Tomáš Kordoš
  • 329
  • 2
  • 12

1 Answers1

7

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)

You can use Array.flat(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26