Skip to content

Instantly share code, notes, and snippets.

@creative-cranels
Created December 12, 2018 10:56
Show Gist options
  • Save creative-cranels/89136142056f32d61717a50bf4d5e035 to your computer and use it in GitHub Desktop.
Save creative-cranels/89136142056f32d61717a50bf4d5e035 to your computer and use it in GitHub Desktop.
arbitrarily array to nested
// initial array
const a = [1, [2, 3], ["4", [5, 6, 7, [8, 9]]]];
// recursive function wich takes result array and inserts non-array items to it
function rec(result, arr) {
for (let i=0; i<arr.length; ++i) {
if (Array.isArray(arr[i]))
rec(result, arr[i]);
else
result.push(arr[i]);
}
};
const result = [];
rec(result, a);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment