Tim Rashford
Tim Rashford

Reputation: 11

Compare two array's and disable element if the id's are equal

I have two array in the state and both have id's.

arrOne = [2,6,8]
arrTwo = [3, 8, 4]

if some array have the same value (In this case 8) i would like to disable all the buttons that have this equal value.

I tried like this but i'm not getting it

button = () => {
const checkId = this.state.arrOne.filter(arr => arr.includes(this.state.arrTwo.map(data => data.id))


if(checkedId){
return <Button disable />
} 

return <Button />
}
render(){
this.button()
}

I have buttons with all the array, if the array one is equal to the array two i want to disable this specific button that is equal Any ideas ?

thanks for the help

Upvotes: 0

Views: 378

Answers (2)

sneas
sneas

Reputation: 1062

This should help.

const isDisabled = this.state.arrOne.some(item => this.state.arrTwo.includes(item));

return <Button disabled={isDisabled} />;

Upvotes: 1

Rajeev Radhakrishnan
Rajeev Radhakrishnan

Reputation: 1004

Find the intersection of array and do the logics

var setOne = [2,6,8];
var setTwo = [3, 8, 4]

var hasDuplicateValues = [...new Set(setOne)].filter(item => setTwo.includes(item));

if(hasDuplicateValues.length > 0) {
    // Disable button
}
else {
    // Enable button
}

Upvotes: 0

Related Questions