Skip to content

Instantly share code, notes, and snippets.

@carl-parrish
Last active December 25, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carl-parrish/e7294641065ffef4d05c268b36c5837c to your computer and use it in GitHub Desktop.
Save carl-parrish/e7294641065ffef4d05c268b36c5837c to your computer and use it in GitHub Desktop.
FlattenArray.js
/********************
** 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment