find 命令示例
JavaScript find()方法 (JavaScript find() method)
find() method is used to get the first element from an array which passes the given test (condition).
find()方法用于从通过给定测试(条件)的数组中获取第一个元素。
Syntax:
句法:
array.find(function, [value]);
Parameters: A function name and an optional value to be tested with all elements.
参数:要与所有元素一起测试的功能名称和可选值 。
Ref: JS Array find() function
参考: JS Array find()函数
Return value: A first element which passes the test.
返回值:通过测试的第一个元素。
Example:
例:
//function for positive value
function getPositive(n){
return n>=0;
}
Input:
var numbers = [-30, -10, 20, 20, 30, 0, 40];
Function call:
var item = numbers.find(getPositive);
Output:
20
JavaScript code to find first positive number from an array using Array.find() method
JavaScript代码使用Array.find()方法从数组中查找第一个正数
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
//function for positive value
function getPositive(n){
return n>=0;
}
var numbers = [-30, -10, 20, 20, 30, 0, 40];
//find first positive number
var item = numbers.find(getPositive);
document.write("first positive number is " + item + "<br>");
</script>
</body>
</html>
Output
输出量
first positive number is 20
翻译自: https://www.includehelp.com/code-snippets/array-find-method-with-example-in-javascript.aspx
find 命令示例