David Roman A.
David Roman A.

Reputation: 13

how can i extract arrays from one array & convert them in object and put them in a new array?

Imagine you have this :


[[{id: 1}], [{id: 2}]]

Is posible to convert the previous array in something like this? :

[{id:1}, {id:2}]

Upvotes: 1

Views: 50

Answers (2)

Sanket Shah
Sanket Shah

Reputation: 3091

One way is:

console.log([[{id: 1}], [{id: 2}]].map(arr => arr[0]))

Another way is Array.prototype.flat():

const arr1 = [[{id: 1}], [{id: 2}]]

console.log(arr1.flat())

Upvotes: 2

Kelvin Schoofs
Kelvin Schoofs

Reputation: 8718

You can simply use Array.prototype.flat for that:

console.log([
  [{
    id: 1
  }],
  [{
    id: 2
  }]
].flat());

Upvotes: 3

Related Questions