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

JavaScript array determines whether it contains

WBOY
Release: 2023-05-16 11:02:07
Original
1838 people have browsed it

In JavaScript, an array is a very common data structure that can store multiple values ​​and can be manipulated using various array methods. In actual development, we often need to find whether an array contains a certain value. This article will introduce how to determine whether an array contains a certain value in JavaScript.

Method 1: Use the indexOf() function

The indexOf() function is a built-in function provided by JavaScript arrays, which can be used to search for the position of a specific value in the array. When the value is searched for, the function returns the index position of the value in the array. If the value is not in the array, -1 is returned.

Therefore, we can use the indexOf() function to determine whether the array contains a certain value. The specific implementation is as follows:

let arr = [1, 2, 3, 4, 5];
let target = 3;
if (arr.indexOf(target) !== -1) {
  console.log("数组包含" + target);
} else {
  console.log("数组不包含" + target);
}
Copy after login

In the above code, if the array arr contains an element with a value of 3, then "array contains 3" will be output, otherwise "array does not contain 3" will be output.

It should be noted that the index value returned by the indexOf() function starts from 0. Therefore, when using the indexOf() function, if you need to determine whether it contains an element, you need to determine whether it is equal to -1. In addition, when using this function, you need to pay attention to the matching of data types. If you want to search for a string, you need to use the strict equality operator (===), as shown in the following code:

let arr = ["apple", "banana", "orange"];
let target = "banana";
if (arr.indexOf(target) !== -1) {
  console.log("数组包含" + target);
} else {
  console.log("数组不包含" + target);
}
Copy after login

In the above code, We changed the content in the array to a string, and the search target is also a string. If you use the "==" operator during judgment, you will get incorrect results.

Method 2: Use the includes() function

The includes() function is another built-in function provided by JavaScript arrays. It can be used to determine whether an array contains a certain element. The return value of the includes() function is a Boolean value. If the element is included in the array, it returns true, otherwise it returns false.

We can use the includes() function to determine whether the array contains a certain value. The specific implementation is as follows:

let arr = [1, 2, 3, 4, 5];
let target = 3;
if (arr.includes(target)) {
  console.log("数组包含" + target);
} else {
  console.log("数组不包含" + target);
}
Copy after login

In the above code, if the array arr contains an element with a value of 3, then "array contains 3" will be output, otherwise "array does not contain 3" will be output.

It should be noted that the return value of the includes() function is true or false, and the index value of the element in the array will not be returned. In addition, when using this function, you need to pay attention to the matching of data types. If you want to search for a string, you need to use the strict equality operator (===), as shown in the following code:

let arr = ["apple", "banana", "orange"];
let target = "banana";
if (arr.includes(target)) {
  console.log("数组包含" + target);
} else {
  console.log("数组不包含" + target);
}
Copy after login

Method 3: Use ES6 find() function

In ES6, JavaScript arrays provide a new function find(), which can be used to find elements in the array that meet certain conditions. The parameter of the find() function is a callback function, which can accept three parameters: element value, element index, and the array itself. The callback function needs to return a Boolean value. If it is true, it means that an element that meets the condition has been found, and the find() function will return the element. If the callback function returns false, continue searching.

We can use the find() function to determine whether the array contains a certain value. The specific implementation is as follows:

let arr = [1, 2, 3, 4, 5];
let target = 3;
if (arr.find(item => item === target)) {
  console.log("数组包含" + target);
} else {
  console.log("数组不包含" + target);
}
Copy after login

In the above code, we use the arrow function to implement the callback function. The parameter of the arrow function is item, and the code item === target indicates whether the target element is found. If the array arr contains an element with a value of 3, it will output "array contains 3", otherwise it will output "array does not contain 3".

It should be noted that the find() function returns the found element. If it is not found, it returns undefined. In addition, when using this function, you need to pay attention to the syntax of the callback function. If the callback function does not return or returns false, the first undefined element will be found, which may lead to incorrect results.

Summary

The above introduces three methods for determining whether a JavaScript array contains a certain value: indexOf() function, includes() function, and ES6 find() function. Each of these three methods has its own advantages and disadvantages, and you need to choose the appropriate method according to the actual situation. In addition, you need to pay attention to the matching of data types and the syntax of the callback function when using it.

The above is the detailed content of JavaScript array determines whether it 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