ArcMech
ArcMech

Reputation: 475

Remove multiple items in filter method React/JS

I have to manipulate two arrays. And I have some issues with removing multiply items. Let assume we have two arrays:

const [array1, setArray1] = useState([1,3,5])

const [array2, setArray2] = useState([2,4,6])

And there is a function for one of them to remove number from array:

const someArray = [4]

const toArray1 = (someArray) => { setArray2(array2.filter(e => e !== someArray[0]))

This code works if one element is removed from array, however I would like to obtain that I can remove multiply numbers from array. For example const someArray = [2,4,6] allows me to get array2 = []

I wonder is there is a good way to chain filter and map (or forEach) to filter all values from given array.

Upvotes: 1

Views: 2799

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386560

You could take Array#includes for a check with an array.

array2.filter(e => !someArray.includes(e))

Upvotes: 4

Related Questions