flatten a nested array

PHOTO EMBED

Wed Apr 05 2023 22:15:04 GMT+0000 (Coordinated Universal Time)

Saved by @azcraze #javascript #flatten #array

const nestedArray = [1, [2], [[3], 4], 5];

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

flatten(nestedArray);
content_copyCOPY

https://codetogo.io/how-to-flatten-a-nested-array-in-javascript/