JSFiddle - React, Tailwind, and code Playground

by bogdan_vq

HTML

<div id="r"></div>

JavaScript

/*
 * collect rec. leafs of tree in accumulator
 */
function rec(tree, acc){
      if((typeof tree) === 'number'){
          acc.push(tree);
      }
      if(tree.length > 0){
          rec(tree[0], acc);
          rec(tree.slice(1, tree.length), acc);
     }
}


function flatten (tree){
     let flat = [];
     rec(tree, flat)

     return flat;
}

var a = [[1,2,[3]],4];
var result = flatten(a);

$('#r').text(result.toString());