Created
July 22, 2016 16:08
-
-
Save panda01/4d391eaab9a6602c61f5c0824be045b2 to your computer and use it in GitHub Desktop.
Flatten an arbitrarily nested 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
function flattenArr(nestedArr, retVal = []) { | |
nestedArr.forEach(val => { | |
// this should be the only check since no object are expected | |
const isArr = (typeof val === 'object'); | |
if(isArr) { | |
flattenArr(val, retVal); | |
} else { | |
retVal.push(val); | |
} | |
}); | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment