cachaito
2/8/2016 - 9:00 AM

Flattening arrays #1

var myArray = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; // we want to achieve: [1, 2, 3, 4, 5, 6, 7, 8, 9]

//#1
var myNewArray = [].concat.apply([], myArray);

//#2 solution is less performant
var myNewArray = myArray.reduce(function(prev, curr) {
  return prev.concat(curr);
});

//#3 with ES6 and spread operator
var myNewArray = myArray.reduce((acc, it) => [...acc, ...it], []);