Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zoryana94
Created March 29, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoryana94/14b3e849de1c18f959c5c6e3c9ff3806 to your computer and use it in GitHub Desktop.
Save zoryana94/14b3e849de1c18f959c5c6e3c9ff3806 to your computer and use it in GitHub Desktop.
Flatten array
const arr = [[1,2,[3]],4];
function flattenArray(arr = []) {
let flattenedArray = [];
arr.forEach(item => {
if (Array.isArray(item)) {
flattenedArray = [...flattenedArray, ...flattenArray(item)];
} else {
flattenedArray.push(item);
}
});
return flattenedArray;
}
flattenArray(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment