Skip to content

Instantly share code, notes, and snippets.

@manubamba
manubamba / flatten.js
Created May 25, 2016 02:39
Code to flatten an array of the form [1, [2, [3]], 4] to [1, 2, 3, 4]
const MAX_ARRAY_INDEX = Math.pow(2, 53) - 1;
const _flatten = (arr) => {
let result = [];
for(let elem of arr) {
if (Array.isArray(elem)) {
elem = flatten(elem);
result = result.concat(elem);
} else {
result.push(elem);