Last active
September 16, 2016 07:42
-
-
Save ronen-e/ed1d0cc546ead1e91059eb509f41c8b4 to your computer and use it in GitHub Desktop.
Flatten array
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
| /** | |
| * Flattens native JS Array objects. | |
| * Uses the native Array.isArray method, therefore array like objects (e.g Arguments object) need to be converted to Array | |
| * instances for this function to work on them (e.g ...arguments) | |
| * @param {Array} list | |
| * @param {Array} newList - not required for the first call of function | |
| * @return {Array} newList | |
| */ | |
| function flatten(list, newList = []) { | |
| for (var i = 0, length = list.length; i < length; i++) { | |
| if (Array.isArray(list[i])) { | |
| flatten(list[i], newList); | |
| } else { | |
| newList.push(list[i]); | |
| } | |
| } | |
| return newList; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment