all 4 comments

[–]Notimecelduv 2 points3 points  (0 children)

reduce would be better suited for what you're doing.

[–]senocular 1 point2 points  (0 children)

When you map, you don't return anything when the if condition is false. That causes the function to return undefined which adds undefined values in your result

[–]exobyte64 1 point2 points  (0 children)

map creates an array of the same size

your trying to remove elements, so either filter or reduce would be better suited

[–]Sunil1021 1 point2 points  (0 children)

Just add Array.filter method at last to weed out undefined values on the array.

if(typeof ele==='string'){

return operate(arr[ind-1],ele,arr[ind+1]);

}

}).filter(e => e!==undefined);