1

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

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

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

MH2K9
  • 11,772
  • 7
  • 31
  • 48
  • 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
  • var newArray = oldArray.map(innerArray=>innerArray[0])) – nivendha Aug 05 '19 at 10:09
  • 3
    Possible duplicate of [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – AZ_ Aug 05 '19 at 10:17

3 Answers3

4

Use Array.flat():

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

const output = arr.flat();

console.log(output);
Fraction
  • 9,011
  • 4
  • 19
  • 38
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
  • 14,237
  • 29
  • 40
1

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

Maciej Trojniarz
  • 682
  • 4
  • 14