Created
December 12, 2018 10:56
-
-
Save creative-cranels/89136142056f32d61717a50bf4d5e035 to your computer and use it in GitHub Desktop.
arbitrarily array to nested
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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