Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const flatten = function(array, flattenArray = []) {
- for (let i = 0; i < array.length; i++) {
- const value = array[i];
- if (Array.isArray(value)) {
- flatten(value, flattenArray);
- } else {
- flattenArray.push(value);
- }
- }
- return flattenArray;
- };
- const a = [1, [2, [3, 4, [5, [6, 7]]]], 8, 9];
- console.log(flatten(a)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement