JavaScript: Flatten an array using the recursive method

Table of contents

No heading

No headings in the article.

let output = [];
function flattenArray(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      flattenArray(arr[i]);
    } else {
      output.push(arr[i]);
    }
  }
  return output;
}

console.log(flattenArray([[[1, ['A']], 2, 3, 'B'], [4, 5, 'C'], 6]));
Output:
[1, 'A', 2, 3, 'B', 4, 5, 'C', 6]

Execute above code here