-2

I have an array like this:

let arr = ['liverpool', 'arsenal', 'man utd', 'chelsea', 'tottenham', 'crystal palace', 'madrid', 'barcelona'];

and I want to remove items, let's say 'arsenal' and 'chelsea', but this needs to be dynamic.

I have tried the following, where items is an array but unfortunately it didn't work:

function removeItems(items) {
   arr.filter(item => {
      return !arr.includes(items)
   });
}

removeItems(['arsenal', 'chelsea']);
7
  • is items an array? Commented Aug 29, 2019 at 16:07
  • @RohitKashyap sorry was just updating the question Commented Aug 29, 2019 at 16:08
  • why const ... don't you think it should be var Commented Aug 29, 2019 at 16:09
  • @mohitesachin217 updated Commented Aug 29, 2019 at 16:09
  • filter returns a new array, it doesn't modify the array you call it on. So removeItem would need to return the result, and the call would have to assign the result to something. Commented Aug 29, 2019 at 16:10

1 Answer 1

2

I think what you are trying to do is this:

function removeItems(items) {
   return arr.filter(item => {
      return !items.includes(item)
   });
}

const newArr = removeItems(['arsenal', 'chelsea']);
Sign up to request clarification or add additional context in comments.

3 Comments

No, see my comment.
arr.filter returns the new array, not mutates it, edited
this is exactly what I was looking for. Thanks very much for your help, answer makes sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.