How to flatten a nested array in JavaScript

Arrays
Last updated Feb 21, 2018
const nestedArray = [1, [2], [[3], 4], 5];

const flatten = nestedArray =>
  nestedArray.reduce(
    (flat, item) => flat.concat(Array.isArray(item) ? flatten(item) : [item]),
    []
  );

flatten(nestedArray);
[1, 2, 3, 4, 5 ]