乐闻世界logo
搜索文章和话题

How do I check if an array includes a value in JavaScript?

4个答案

1
2
3
4

In JavaScript, there are several ways to check if an array contains a specific value. Here are some common methods:

1. includes Method

Introduced in ES6/ECMAScript 2015, the Array.prototype.includes method checks whether an array contains a specific element. This method returns a boolean value.

javascript
let fruits = ['apple', 'banana', 'mango', 'orange']; let containsMango = fruits.includes('mango'); // returns true let containsCherry = fruits.includes('cherry'); // returns false

2. indexOf Method

Before ES6, the indexOf method was commonly used to check for the presence of an element in an array. If the array contains the specified element, indexOf returns its index (starting from 0); otherwise, it returns -1.

javascript
let fruits = ['apple', 'banana', 'mango', 'orange']; let mangoIndex = fruits.indexOf('mango'); // returns 2 let containsMango = mangoIndex !== -1; // returns true if mango is present let cherryIndex = fruits.indexOf('cherry'); // returns -1 let containsCherry = cherryIndex !== -1; // returns false

3. find or findIndex Methods

The find method retrieves the value of the first element in the array that satisfies the provided test function. If no matching element is found, it returns undefined. The findIndex method is similar to find, but it returns the index of the found element; if not found, it returns -1.

javascript
let fruits = ['apple', 'banana', 'mango', 'orange']; let containsMango = fruits.find(fruit => fruit === 'mango') !== undefined; // returns true let containsCherry = fruits.findIndex(fruit => fruit === 'cherry') !== -1; // returns false

4. some Method

The some method tests whether at least one element in the array passes the provided test function, returning a boolean value.

javascript
let fruits = ['apple', 'banana', 'mango', 'orange']; let containsMango = fruits.some(fruit => fruit === 'mango'); // returns true let containsCherry = fruits.some(fruit => fruit === 'cherry'); // returns false

These are all valid methods to check if an array contains a specific value. Which one to use primarily depends on your specific requirements and JavaScript version compatibility. Typically, the includes method is the most straightforward and readable approach, but if you need to support older browsers, you might need to use indexOf. The find, findIndex, and some methods offer greater flexibility, allowing you to use functions to determine if a value is present.

2024年6月29日 12:07 回复

In JavaScript, you can use various methods to check if an array contains a specific value. Here are some common methods:

1. includes Method

javascript
let array = [1, 2, 3, 4, 5]; let value = 3; if (array.includes(value)) { console.log('array contains the value'); } else { console.log('array does not contain the value'); }

The includes method checks if the specified value exists in the array and returns a boolean value (true or false).

2. indexOf Method

javascript
let array = [1, 2, 3, 4, 5]; let value = 3; if (array.indexOf(value) !== -1) { console.log('array contains the value'); } else { console.log('array does not contain the value'); }

The indexOf method finds the first index of the specified value in the array and returns it. If the array does not contain the value, it returns -1.

3. find Method

javascript
let array = [1, 2, 3, 4, 5]; let value = 3; if (array.find(element => element === value) !== undefined) { console.log('array contains the value'); } else { console.log('array does not contain the value'); }

The find method returns the first element in the array that satisfies the provided testing function. If no matching element is found, it returns undefined.

4. some Method

javascript
let array = [1, 2, 3, 4, 5]; let value = 3; if (array.some(element => element === value)) { console.log('array contains the value'); } else { console.log('array does not contain the value'); }

The some method tests whether at least one element in the array passes the provided test function. It returns a boolean value.

Notes

  • The includes method does not distinguish between NaN values in the array, meaning [NaN].includes(NaN) returns true.
  • The includes and indexOf methods do not check for equality of object references or nested arrays; they are only suitable for comparing primitive types.
  • The find and some methods can be used with callback functions to implement more complex checks, including deep comparison of objects.

When using these methods, choose the most appropriate one based on the nature of the check you need to perform and whether the target browser supports these methods. For example, the includes method was introduced in ES2016 (ES7) and is not supported by some older browsers.

2024年6月29日 12:07 回复

ECMAScript 7 introduced Array.prototype.includes.

It can be used as follows:

javascript
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false

It also accepts an optional second parameter fromIndex:

javascript
[1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true

Unlike indexOf, which uses Strict Equality Comparison, includes uses SameValueZero for comparison. This means you can detect if an array contains NaN:

javascript
[1, 2, NaN].includes(NaN); // true

Unlike indexOf, includes does not skip missing indices:

javascript
new Array(5).includes(undefined); // true

It can be polyfilled to work across all browsers.

2024年6月29日 12:07 回复

indexOf may be used, but it is an ECMA-262 standard JavaScript extension; therefore it may not appear in other implementations of the standard.

javascript
[1, 2, 3].indexOf(1) => 0 ["foo", "bar", "baz"].indexOf("bar") => 1 [1, 2, 3].indexOf(4) => -1

As far as I can see, Microsoft has not provided an alternative for indexOf, but you can add similar functionality to arrays in Internet Explorer (and other unsupported browsers) if you wish, as demonstrated by a Google search (for example, this resource: [http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/]).

2024年6月29日 12:07 回复

你的答案