Skip to content

Instantly share code, notes, and snippets.

@Ivanca
Created November 28, 2017 22:07
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 Ivanca/a0f247cf4a9add9958686174bd5ebcf1 to your computer and use it in GitHub Desktop.
Save Ivanca/a0f247cf4a9add9958686174bd5ebcf1 to your computer and use it in GitHub Desktop.
// Flattens an array, e.g. [[1,2,[[[3]]]],4] => [1, 2, 3, 4]
var flattenArray = (array) => {
let initialValue = [];
return array.reduce((result, ele) => {
if (Array.isArray(ele)) {
let flat = flattenArray(ele);
return result.concat(flat);
} else {
result.push(ele);
return result;
}
}, initialValue);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment