Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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