DEV Community

Discussion on: Demystifying Array.prototype.flat

Collapse
 
manimanis profile image
Mohamed Anis MANI • Edited

To flatten your code in JavaScript you can use the old recursive way.

const flatArray = (arr) => {
  let res = [];
  for (let item of arr) {
    if (item instanceof Array) {
      item = flatArray(item);
      res = res.concat(item);
    } else {
      res.push(item);
    }
  }
  return res;
}