Check if item exists in array React
it should be:
handleCheck(val) { return this.state.data.some(item => val.name === item.name);}
because val
here is an Object not a String.
Check this out:https://www.webpackbin.com/bins/-Kpo0Rk6Z8ysenWttr7q
While searching for Check value exists in an array in React, I was landed in this page and would like to give a solution (apart from this question) for others who think there is any special case to check for a value in an array using React.
You can rightly use the default JavaScript method too. There is nothing special when it comes to React.
var arr = ["steve", "bob", "john"];console.log(arr.indexOf("bob") > -1); //true
Thank you.
Use the includes()
method on the array instance.
console.log(['red', 'green'].includes('red'))console.log(['red', 'green'].includes('blue'))