I want to check if a value in array1 exists in an object in array2.
array1:
[2,5,1]
array2:
[
{ value: 1, name: 'Monday', isSelected: false },
{ value: 2, name: 'Tuesday', isSelected: false },
{ value: 3, name: 'Wednesday', isSelected: false },
{ value: 4, name: 'Thursday', isSelected: false },
{ value: 5, name: 'Friday', isSelected: false },
]
What I want to achieve is to check array1 against array2 property named value. If the value of an object in array2 is included in array1, the isSelected property should be updated to true. I've tried:
this.setState(prevState => ({
...prevState,
array2: prevState.array2.map(el => {
if (el.value === array2) {
return {
...el,
isSelected: !el.isSelected
}
}
return el;
})
}))
array2.map(a => a.value)and then loop through it if it contains array1 valuesarray2starts out withisSelectedproperties that aretrue, or can that never happen? Would you want those properties to remain true, or would you want to set them tofalseif not inarray1?