0

I have an an array of objects and arrays of objects

const a = [[{'k': 'b'}, {'a1': 'c1'}], {'a': 'c'}];

And what I want to have is just array of objects, like

const a = [{'k': 'b'}, {'a1': 'c1'}, {'a': 'c'}];  

I tried to sort out element of a, which are arrays (0 element in this example) and pull out objects separately

const a_arr = [];

for (let i = 0; i < a.length; i++) {
 for(let j = 0; j < a[i].length; j++) {
 a_arr.push(a[i][j]);
 }
}

It works fine, but I don't know what must be my second step? I need to pull out from array just objects (in this case a[1]) and then concat to arrays of object, but how could I achieve this?

Or, maybe there is better way to do all this?

Anna F
  • 1,583
  • 4
  • 22
  • 42

1 Answers1

2

Use Array#reduce and concat elements using Array#concat

const a = [
  [{
    'k': 'b'
  }, {
    'a1': 'c1'
  }], {
    'a': 'c'
  }
];

console.log(a.reduce((initial, elem) => initial.concat(elem), []));
Rayon
  • 36,219
  • 4
  • 49
  • 76