Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge N arrays containing arrays in javascript

I have an array which contains arrays too. this array is template enter image description here

template has 2 arrays, but 2 is not fixed, it can go on N number of arrays which also contains arrays. this is what I tried:

const template = health_pack_templates.map((healthpackItem, i) => {
  return healthpackItem.health_pack_templates
});
console.log('template', template);
var c = [];
  for (var i = 0; i >= template.length; i++) {
      c.push.apply(template[i]);
  }
console.log('c',c)

c does only returns [] and not an array with 0,1,2,3,4,5,6,7,8,9 arrays inside. What am I doing wrong?

what I want to happen should be like this: [array 0,1,2,3,4,5,6,7,8,9] after it is merged.

like image 728
eibersji Avatar asked Jan 01 '23 03:01

eibersji


1 Answers

Try using flat() method

The flat() method which creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

like image 64
User863 Avatar answered Jan 09 '23 23:01

User863