admin管理员组

文章数量:1400599

I'm trying to remove all items if they match with array values but it's removing only one item. How can i remove all items with filter method or what is the best way to achieve this.

let data = [
    {
        id: '1',
        title: 'ABC'
    },
    {
        id: '2',
        title: 'DEF'
    },
    {
        id: '3',
        title: 'GHI'
    },
    {
        id: '4',
        title: 'JKL'
    },
    {
        id: '5',
        title: 'MNO'
    }
]


data = data.filter(post => {

    let remove = ['2', '4', '5']

    for(let i = 0; i < remove.length; i++) {
        return post.id !== remove[i]
    }

})

console.log(data)

Thanks

I'm trying to remove all items if they match with array values but it's removing only one item. How can i remove all items with filter method or what is the best way to achieve this.

let data = [
    {
        id: '1',
        title: 'ABC'
    },
    {
        id: '2',
        title: 'DEF'
    },
    {
        id: '3',
        title: 'GHI'
    },
    {
        id: '4',
        title: 'JKL'
    },
    {
        id: '5',
        title: 'MNO'
    }
]


data = data.filter(post => {

    let remove = ['2', '4', '5']

    for(let i = 0; i < remove.length; i++) {
        return post.id !== remove[i]
    }

})

console.log(data)

Thanks

Share Improve this question asked Dec 28, 2019 at 12:19 KeyKey 7582 gold badges9 silver badges17 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

you should return false if you want to remove item from array

let data = [
    {
        id: '1',
        title: 'ABC'
    },
    {
        id: '2',
        title: 'DEF'
    },
    {
        id: '3',
        title: 'GHI'
    },
    {
        id: '4',
        title: 'JKL'
    },
    {
        id: '5',
        title: 'MNO'
    }
]
let remove = ['2', '4', '5']

data = data.filter(post => {
return !remove.includes(post.id);
})

console.log(data)

All the notice are in the snippet's ment

let data = [ { id: '1', title: 'ABC' }, { id: '2', title: 'DEF' }, { id: '3', title: 'GHI' }, { id: '4', title: 'JKL' }, { id: '5', title: 'MNO' } ]

const remove = ['2', '4', '5']

// `indexOf` is from ES5
data = data.filter(post => remove.indexOf(post.id) === -1)
console.log(data)

// `includes` is from ES7
data = data.filter(post => !remove.includes(post.id))
console.log(data)

// this will recreate the array ['2', '4', '5'] 5 times
data = data.filter(post => !['2', '4', '5'].includes(post.id))
console.log(data)

There is no need to use for loop inside of filter.

Instead it is possible to use some method inside of filter. The some method checks whether at least one element satisfies condition inside of provided function. So unnecessary iteration will be avoided:

data.filter(f => !remove.some(s => s == f.id))

An example:

let data = [
    {
        id: '1',
        title: 'ABC'
    },
    {
        id: '2',
        title: 'DEF'
    },
    {
        id: '3',
        title: 'GHI'
    },
    {
        id: '4',
        title: 'JKL'
    },
    {
        id: '5',
        title: 'MNO'
    }
]

let remove = ['2', '4', '5']

console.log(data.filter(f => !remove.some(s => s == f.id)));

I'll suggest using includes rather then a nested for loop.

You should also move the remove var outside of the loop, so it's not reinitialised every time.

The callback to the filter method is a predicate. If the condition evaluates to true, the current value in the iteration will be returned. In your case, you want to return if the current value is not in the remove array.

let data = [
  {
      id: '1',
      title: 'ABC'
  },
  {
      id: '2',
      title: 'DEF'
  },
  {
      id: '3',
      title: 'GHI'
  },
  {
      id: '4',
      title: 'JKL'
  },
  {
      id: '5',
      title: 'MNO'
  }
]

const remove = ['2', '4', '5']

data = data.filter(post => {
  return !remove.includes(post.id)
})

console.log(data)

本文标签: Javascript filter methodRemove all Items with matching values in arrayStack Overflow