/********************
** Write some code, that will flatten an array
** of arbitrarily nested arrays of integers
** into a flat array of integers.
** e.g. [[1,2,[3]],4] -> [1,2,3,4]
************************/
var answer = [];
function flatten(source){
source.map(val => Array.isArray(val)? flatten(val) : answer.push(val));
return answer;
}
const myArray = [[1,2,[3]],4];
console.log(flatten(myArray)); // [1,2,3,4]
Last active
December 25, 2017 16:14
-
-
Save carl-parrish/e7294641065ffef4d05c268b36c5837c to your computer and use it in GitHub Desktop.
FlattenArray.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment