JSFiddle - React, Tailwind, and code Playground

by adelura

JavaScript

var input = [1, {a: [2, [3]]}, 4, [5, [6]], [[7], 8, 9], 10, [5, 6]];
// var output = [1, {a: [2, [3]]}, 4, 5, 6, 7, 8, 9, 10];

function flatten(arr) {
	const result = [];
  const arrays = [arr];

  while (arrays.length) {
    const currentArray = arrays[0];
    
    if (currentArray.length === 0) {
    	arrays.shift();
      continue;
    }
    
  	let current = currentArray.shift();
    
    if (Array.isArray(current)) {
    	arrays.unshift(current);
    } else {
    	result.push(current);
    }
  }
  
  return result;
}