Home  >  Article  >  Web Front-end  >  JS array learning to check whether a given element exists

JS array learning to check whether a given element exists

青灯夜游
青灯夜游Original
2021-08-30 18:06:301478browse

In the previous article, we introduced three methods for JavaScript to return the first element that satisfies a given condition. If you are interested, you can click on the link to read → "JS Array Learning: Return the first element that satisfies a given condition. elements》. This time we continue to learn JavaScript arrays and talk about how to use JS to detect whether a specified value exists in an array. Friends in need can learn about it~

The main content of today’s article is: given a value , traverse the array and check whether the array contains the value. Simply put, it checks whether one or more array elements in the array have a value equal to this value.

The following article will take you through 4 methods and introduce them to you in detail through code examples. Without further ado, let’s start directly~

Method 1: Use for loop

Implementation idea: use for statement to traverse the array, in each In the second loop, === is used to determine whether the array element is a given value. When the first element meets the condition, the given element exists in the array, a prompt is given, and then the break statement is used to exit the entire loop. .

Let’s learn more about it through examples: Determine whether the fruits array contains the given element "Durian"

var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];
for(var i=0;i<fruits.length;i++){
	if (fruits[i] === "榴莲") {
		console.log("指定元素‘ "+fruits[i]+" ’是存在的");
		break;
	}
}

The output result is:

JS array learning to check whether a given element exists

Method 2: Using the includes() method of the array

includes() method is used to determine whether an array contains a specified Value, returns true if it is, false otherwise.

Syntax: arr.includes(searchElement [, fromIndex])

  • searchElement is required. The element value to be found.

  • fromIndex Optional. Start looking for searchElement at this index. If negative, the search starts at the index of array.length fromIndex in ascending order. Default is 0.

Let’s learn more about it through examples: Determine whether the fruits array contains the given element "Durian"

var fruits = [&#39;苹果&#39;,"香蕉", &#39;榴莲&#39;, &#39;橘子&#39;, &#39;菠萝蜜&#39;,"梨子"];
if(fruits.includes(&#39;榴莲&#39;)){
	console.log("给定元素是存在的");
}else{
	console.log("给定元素是不存在的");
}

The output result is:

JS array learning to check whether a given element exists

Method 3: Use the some() method of the array

The some() method can detect the contents of the array Whether there are elements that meet the condition. Thinking about it from another angle, it can also be used to detect whether all elements in the array do not meet the specified conditions. If they do not meet the specified conditions, it will return false. If one or more elements match, it will return true.

Syntax: array.every(function callbackfn(Value,index,array),thisValue)

Let’s learn more about it through examples:

function f(value, index, ar) {
    if (value === &#39;橘子&#39;) {
        return true;
    }
}
var fruits = [&#39;苹果&#39;,"香蕉", &#39;榴莲&#39;, &#39;橘子&#39;, &#39;菠萝蜜&#39;,"梨子"];
var b = fruits.some(f);

if (b) {
	console.log("给定元素是存在的");
} else {
	console.log("给定元素是不存在的");
}

The output result is:

给定元素是存在的

Method 4: Using the indexOf() method of the array

indexOf() method can return the array The first occurrence of a specified element in . If the element to be retrieved does not appear, the method returns -1.

Implementation idea: Use this method to check the first occurrence position of the specified value in the array. If the position exists, the given element is included. If -1 is returned, the given element is not contained.

The implementation code is given below:

var fruits = [&#39;苹果&#39;,"香蕉", &#39;榴莲&#39;, &#39;橘子&#39;, &#39;菠萝蜜&#39;,"梨子"];
var b = fruits.indexOf("橘子");

if (b>0) {
	console.log("给定元素是存在的");
} else {
	console.log("给定元素是不存在的");
}

The output result is:

给定元素是存在的

Method 5: Using the lastIndexOf() method of the array

The lastIndexOf() method searches for an element in the array and returns the position where it last occurred. If the element to be retrieved does not appear, the method returns -1.

Implementation idea: Use this method to check the last occurrence position of the specified value in the array. If the position exists, the given element is included; if -1 is returned, the given element is not included.

The implementation code is given below:

var fruits = [&#39;苹果&#39;,"香蕉", &#39;榴莲&#39;, &#39;橘子&#39;, &#39;菠萝蜜&#39;,"梨子"];
var b = fruits.lastIndexOf("葡萄");

if (b>0) {
	console.log("给定元素是存在的");
} else {
	console.log("给定元素是不存在的");
}

The output result is:

给定元素是不存在的

Okay, that’s it for now, you can read it if you need it:javascript video tutorial

The above is the detailed content of JS array learning to check whether a given element exists. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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