Home > Web Front-end > Front-end Q&A > javascript determines whether array contains

javascript determines whether array contains

王林
Release: 2023-05-17 20:47:06
Original
3692 people have browsed it

In JavaScript, it is a very common operation to determine whether an array contains an element. For different needs, we can use different methods to determine whether an array contains a certain element. This article will introduce several common judgment methods.

Method 1: Use the includes() method

In ES6, an includes() method is provided for arrays to determine whether the array contains an element. Its syntax is as follows:

array.includes(searchElement[, fromIndex])

Among them, searchElement represents the element to be found, fromIndex represents the starting position of the search, and the default value is 0.

includes() method returns a Boolean value indicating whether the specified element is included in the array.

The following is a sample code that uses the includes() method to determine whether an array contains an element:

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.includes('apple')); // true
console.log(fruits.includes('banana')); // true
console.log(fruits.includes('pear')); // false
Copy after login

Method 2: Use the indexOf() method

If your code needs it Compatible with ES5 or earlier versions, you can use the indexOf() method to determine whether the array contains an element. Its syntax is as follows:

array.indexOf(searchElement[, fromIndex])

Among them, searchElement represents the element to be found, fromIndex represents the starting position of the search, and the default value is 0.

The indexOf() method returns a number indicating the position of the first occurrence of the specified element in the array. If the element is not contained in the array, -1 is returned.

The following is a sample code that uses the indexOf() method to determine whether an array contains an element:

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.indexOf('apple') !== -1); // true
console.log(fruits.indexOf('banana') !== -1); // true
console.log(fruits.indexOf('pear') !== -1); // false
Copy after login

Method 3: Use the find() method

Also provided in ES6 A find() method, which can be used to obtain the first element in an array that meets the conditions. If there is no matching element in the array, undefined is returned. We can use this to determine whether an array contains an element. The following is a sample code that uses the find() method to determine whether an array contains an element:

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.find(item => item === 'apple') !== undefined); // true
console.log(fruits.find(item => item === 'banana') !== undefined); // true
console.log(fruits.find(item => item === 'pear') !== undefined); // false
Copy after login

The find() method here uses an arrow function, which accepts a parameter item, representing each element in the array. . The return value of the arrow function is whether item is equal to the element to be found. If so, it returns true, otherwise it returns false. Ultimately, the find() method returns whether the search result is undefined, that is, whether there is an element that meets the conditions.

Method 4: Use the some() method

Similar to the find() method, ES6 also provides a some() method, which can be used to determine whether there is a qualified element in the array. element. Returns true if a matching element exists in the array, false otherwise. The following is a sample code that uses the some() method to determine whether an array contains an element:

const fruits = ['apple', 'banana', 'orange'];

console.log(fruits.some(item => item === 'apple')); // true
console.log(fruits.some(item => item === 'banana')); // true
console.log(fruits.some(item => item === 'pear')); // false
Copy after login

The some() method here uses an arrow function, which accepts a parameter item, representing each element in the array. . The return value of the arrow function is whether item is equal to the element to be found. If so, it returns true, otherwise it returns false. Ultimately, the some() method returns whether the search result is true, that is, whether there is an element that meets the conditions.

Summary

This article introduces several common ways to determine whether an array contains an element. They are:

  • includes() method
  • indexOf() method
  • find() method
  • some() method

You can choose one of the methods according to your needs. If you are using ES6 or above, it is recommended to use the includes() method or find() method, which is more intuitive and convenient; if you need to be compatible with ES5 or earlier versions, you can use the indexOf() method or some( ) method.

The above is the detailed content of javascript determines whether array contains. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template