I am trying to flatten a nested array contained in array variable. Everything looks fine in the code but still not working. Please help me what's wrong with my code. According to me this code should return a flatten array like [1,2,3,4].
var array = [1,[2],[3,[[4]]]];
function flatten(array){
return array.map(function(currentElement){
if(Array.isArray(currentElement)){
return flatten(currentElement);
}else{
return currentElement;
}
});
}
console.log(flatten(array)); // [1,[2],[3,[[4]]]]
// expected result => [1,2,3,4]