Created
January 7, 2019 20:39
-
-
Save vinyoliver/0123fcd25790e488b46c856f3c8e2469 to your computer and use it in GitHub Desktop.
Converts a multiArray into a flatArray - Code sample
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
function getFlatArray(multiArray){ | |
let flatArray = []; | |
for(var item of multiArray) { | |
if(item instanceof Array){ | |
flatArray.push(...getFlatArray(item)) | |
}else { | |
flatArray.push(item); | |
} | |
} | |
return flatArray; | |
} | |
const multiArray = [[1,2,[3]],4]; | |
console.log('The result is: ', getFlatArray(multiArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment