Last active
December 25, 2017 16:14
-
-
Save carl-parrish/e7294641065ffef4d05c268b36c5837c to your computer and use it in GitHub Desktop.
FlattenArray.js
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
| ```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