How can I delete the previous and next element from an array while using array.map();
In the code below, when I get to 'crossover' I want to delete both 'SUV' and 'sedan'.
The following code deletes 'sedan' and 'truck' instead of 'sedan' and 'SUV' ;
let arr = ['excavator','SUV','crossover','sedan','truck'] ;
arr = arr.map((ele,ind,ar) => {
if (ele === 'crossover'){
ar.splice(ind+1,1);
ar.splice(ind-1,1);
}
return ele + ": Sold.";
});
return arr; //it produces ['excavator: Sold.','SUV: Sold.','crossover: Sold.',,]
.map(). It sounds like you want a.filter()and a.map(), or you could combine the logic into a single.reduce().