Skip to Main Content

Flatten

Problem URL:Flatten

My Solution

JavaScript

/**
 * @param {Array<*|Array>} arr
 * @return {Array}
 */

function flatten(arr) {
  let flattenedArray = [];

  for (let x of arr) {
    if (!Array.isArray(x)) {
      flattenedArray.push(x);
    } else {
      flattenedArray.push(...flatten(x));
    }
  }

  return flattenedArray;
}

Let's Connect

Twitter GitHub LinkedIn