How to check a list contain a value in react
- Using includes() method:
javascript
const myList = [1, 2, 3, 4, 5];
const valueToCheck = 3;
if (myList.includes(valueToCheck)) {
console.log('Value exists in the list');
} else {
console.log('Value does not exist in the list');
}
- Using indexOf() method:
javascript
const myList = [1, 2, 3, 4, 5];
const valueToCheck = 3;
if (myList.indexOf(valueToCheck) !== -1) {
console.log('Value exists in the list');
} else {
console.log('Value does not exist in the list');
}
- Using find() method:
javascript
const myList = [1, 2, 3, 4, 5];
const valueToCheck = 3;
if (myList.find(item => item === valueToCheck)) {
console.log('Value exists in the list');
} else {
console.log('Value does not exist in the list');
}
Comments
Post a Comment