How to check a list contain a value in react

 

  1. 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'); }
  1. 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');
}
  1. 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

Popular posts from this blog

Basic Web Design with HTML and CSS

Project Web Design Assignment