Javascript Array flatten(shallow)

Description

Javascript Array flatten(shallow)


// Flattens a nested array (the nesting can be to any depth).
// If you pass shallow, the array will only be flattened a single level.

var arr = [[1], 2, [[3, 4], 5], [[[]]],[[[6]]], 7, 8, []];

Array.prototype._flatten = function(shallow) {
  if (shallow) {/*ww  w  . j  av a2  s .  c  o m*/
    return this.reduce(function(a, b) {
      return a.concat(b);
    }, []);
  } else {
    return this.reduce(function(a, b) {
      return a.concat(b.constructor === Array ? b._flatten() : b);
    }, []);
  }
};

arr._flatten();
// [1, 2, 3, 4, 5, 6, 7, 8]

arr._flatten(true);
// [1, 2, [3, 4], 5, [[]], [[6]], 7, 8]



PreviousNext

Related