Skip to content

Instantly share code, notes, and snippets.

@panda01
Created July 22, 2016 16:08
Show Gist options
  • Save panda01/4d391eaab9a6602c61f5c0824be045b2 to your computer and use it in GitHub Desktop.
Save panda01/4d391eaab9a6602c61f5c0824be045b2 to your computer and use it in GitHub Desktop.
Flatten an arbitrarily nested array
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