2

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.',,]
2
  • Modifying an iterable while iterating has the stereotypical issue of messing up your iterator, which then has to be manually adjusted. If possible avoid such scenarios. In your case, the construction of the new array has already passed "SUV", so deleting that won't have the desired effect at that time, but will instead mess up the iterator. Commented Dec 13, 2019 at 18:10
  • If the end result of your array manipulation is an array of a different length, it's often an indicator that you might be better off foregoing the use of .map(). It sounds like you want a .filter() and a .map(), or you could combine the logic into a single .reduce(). Commented Dec 13, 2019 at 18:10

3 Answers 3

2

You can first remove the elements and then map the array, here is an example:

let arr = ['excavator','SUV','crossover','sedan','truck'];
arr.splice(arr.indexOf('crossover') - 1, 3, 'crossover');
arr = arr.map((ele) => {
  return ele + ": Sold."; 
});
console.log(arr); 

The .splice(..) call replaces SUV, crossover and sedan with crossover

Sign up to request clarification or add additional context in comments.

Comments

0

You should not remove elements on a .map interation since the array size will change as well as the array indexes.

You should try something like this:

let arr = ['excavator','SUV','crossover','sedan','truck'] ;
arr = arr.filter(element => element != "crossover");
arr = arr.map(ele => `${ele} :Sold.`);
console.log(arr);

Or, from an old school perspective:

let arr = ['excavator','SUV','crossover','sedan','truck'];
let newArr=[]
for (var i=0; i<arr.length; i++)
  if(arr[i] !== 'crossover')
     newArr.push(`${arr[i]} :Sold.`) 

console.log(newArr);

1 Comment

This code is unuseful, he question about eliminate suv and sedan from the array, not crossover
0

itus has the correct anwer, IMO but for the sake of fun this is an alternative way:

let arr = ['excavator','SUV','crossover','sedan','truck'] ;

let suvIndex = null;
const result = arr.map((item, index) => {
  if (item === "SUV") {
    suvIndex = index;
  }

  if(suvIndex && index <= suvIndex + 2) {
    return null;
  }

  return item;
}).filter(chunk => chunk)

console.log(result);

https://jsfiddle.net/vbz14fw6/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.