Skip to content

Instantly share code, notes, and snippets.

@ronen-e
Last active September 16, 2016 07:42
Show Gist options
  • Select an option

  • Save ronen-e/ed1d0cc546ead1e91059eb509f41c8b4 to your computer and use it in GitHub Desktop.

Select an option

Save ronen-e/ed1d0cc546ead1e91059eb509f41c8b4 to your computer and use it in GitHub Desktop.
Flatten array
/**
* 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