Javascript filter method, Remove all Items with matching values in array

javascript array filter
javascript remove element from array by value
javascript remove object from array
javascript remove object from array by value
javascript array splice
lodash remove item from array
jquery remove item from array by index
remove first element from array javascript

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'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)

Remove all elements contained in another array, Use the Array.filter() method: myArray = myArray.filter( function( el ) { return toRemove.indexOf( el ) < 0; } );. Small improvement, as browser support for� The Lodash Array Remove Method. Sometimes utility libraries are the best way to solve more complex problems. Lodash provides a rich set of array manipulation methods, one being remove. The Lodash remove method works much like the array filter method, but sort of in reverse. It does not save the original array values, but removes matching elements.

All the notice are in the snippet's comment

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)

9 Ways To Remove 🗑 Elements From A JavaScript Array , The JavaScript Array filter method to create a new array with desired If you want to remove multiple items that match your criteria there is a� The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not execute the function for array elements without values. Note: filter() does not change the original array.

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)

The Fastest Way to Remove a Specific Item from an Array in JavaScript, Then I compare performance with a for loop and the .filter method. bit faster, but .filter() is fine for removing all matching values from an array. Store the index of array elements into another array which need to be removed. Use filter() method on the array of elements. Use indexOf() method to select only those elements which are not present in the indexes array. Example 2: This example uses filter() method and indexOf() method to remove multiple elements from array.

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)));

Array.prototype.filter(), The filter() method creates a new array with all elements that pass the test implemented by let newArray = arr .filter( callback ( element [, index , [ array ]])[, thisArg ]) Affecting Initial Array (modifying, appending and deleting). The filter() method creates a new array with all the elements that pass the test implemented by the callback() function. Internally, the filter() method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array.

Remove Element from an Array in JavaScript, To remove a particular element from an array in JavaScript we'll want to first find (if not all) of the utility libraries have a function to remove elements from an array. need to pass a function that determines if the element matches your criteria. filter method, except that it removes the elements from the array you pass and� 0:14 Let's start with the filter method. 0:16 You can use filter to remove items from an array. 0:18 Filter doesn't affect the original array. 0:22 Instead, like other methods you'll learn about here, it returns a newArray. 0:25 In this case, an array without the items you wanted removed.

Array methods, We already know methods that add and remove items from the The splice method is also able to insert the elements without any removals. The syntax is similar to find , but filter returns an array of all matching elements:. If you genuinely want it to be "efficient", you won't use functional type methods like .filter(). Instead you'll use for loops. You can avoid .splice() if the original order doesn't need to be maintained. Or there are ways to make .splice() more efficient if you think there will be many items to remove. – Blue Skies Nov 13 '13 at 15:33

How to Remove Array Duplicates in ES6, When you pass in an array, it will remove any duplicate values. Okay The filter () method creates a new array of elements that pass the conditional we provide. I'm having some issues comparing the elements of two arrays and filtering out matching values. I only want to return array elements that are NOT included within wordsToRemove.