Let variable = [1, [2], [[3]],[[[4]]]]
Output should be: [1, 2, 3, 4]
Dont use flatten function.
Use for loop
function flattenArray(arr) {
var result = [];
for( var i=0; i<arr.length; ++i) {
if (typeof arr[i] === 'number') {
result.push(arr[i]);
}
else {
result = result.concat(flattenArray(arr[i]));
}
}
return result;
}
Comments
Leave a comment