Skip to content

Instantly share code, notes, and snippets.

@vinyoliver
Created January 7, 2019 20:39
Show Gist options
  • Save vinyoliver/0123fcd25790e488b46c856f3c8e2469 to your computer and use it in GitHub Desktop.
Save vinyoliver/0123fcd25790e488b46c856f3c8e2469 to your computer and use it in GitHub Desktop.
Converts a multiArray into a flatArray - Code sample
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