1

I'm trying to bring out object from an array which inside an array

from this [[{…}],[{…}],[{…}],[{…}],[{…}],[{…}]]

into this [{…},{…},{…},{…},{…},{…}]

MH2K9
  • 11,951
  • 7
  • 32
  • 49
Moh Jonaidy
  • 75
  • 1
  • 10
  • what have you tried? try using [Array.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) or [flatMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) – AZ_ Aug 05 '19 at 10:09

3 Answers3

4

Use Array.flat():

const arr = [[{a:1}],[{a:2}],[{a:3}]];

const output = arr.flat();

console.log(output);
Fraction
  • 11,668
  • 5
  • 28
  • 48
2

Simply flatten the array:

const arr = [[{a:1}],[{a:2}],[{a:3}]];
const flattened1 = [].concat(...arr); // With destructuring
const flattened2 = arr.flat(); // with Array.flat();
console.log(flattened1);
console.log(flattened2);
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
1

You can always use flatMap: arr.flatMap(x => x)

Maciej Trojniarz
  • 702
  • 4
  • 14