Home  >  Article  >  Web Front-end  >  How to use some function

How to use some function

不言
不言Original
2019-02-15 15:32:138813browse

The some() function is used to check whether at least one element in the array meets the conditions for parameter function checking. Its usage syntax is "arr.some(arg_function(element, index, array), thisArg)" .

How to use some function

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.

arr.some() function is used to check whether at least one element in the array meets the conditions of the parameter function check. If one element meets the conditions, the expression returns true, and the remaining elements will not be Performs a test and returns false if there is no element that meets the condition. Let's look at the specific use of some functions.

Let’s first take a look at the syntax of the some() function

arr.some(arg_function(element,index,array),thisArg)

array: the array that calls the .some() function.

index: The index of the current element processed by the function

element: The current element being processed by the function.

Let’s look at the specific example of some() function

The code is as follows

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script> 
function checkAvailability(arr, val)  
{ 
  return arr.some(function(arrVal)  
  { 
    return val === arrVal; 
  }); 
} 
function func()  
{ 
    var arr = [2, 5, 8, 1, 4] 

    document.write(checkAvailability(arr, 2)); 
    document.write("<br>"); 
    document.write(checkAvailability(arr, 87)); 
} 
func(); 
</script> 
</body>
</html>

The running results are as follows:

true
false
## The #some() function checks for 2 and 87 in the array. Since there are only 2 available, the function returns true on the first query and false on the second query.

This article ends here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use some function. 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