JSFiddle - React, Tailwind, and code Playground
by glebcha
JavaScript
function flattenArray(arr, res=[], temp=[]) {
return arr.reduce((acc, item) => {
if (!Array.isArray(item)) {
acc.push(item);
} else {
temp = item.slice();
while (temp.length > 0) {
flattenArray(temp.splice(0,1), acc);
}
}
return acc;
}, res);
}
const input = [1,2,[[[22],[3]]],[4,5]];
const result = iterate(input);
console.log('RESULT', input, result)