javascript
find 命令示例_数组find()方法以及JavaScript中的示例
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()方法用于從通過給定測試(條件)的數(shù)組中獲取第一個元素。
Syntax:
句法:
array.find(function, [value]);Parameters: A function name and an optional value to be tested with all elements.
參數(shù):要與所有元素一起測試的功能名稱和可選值 。
Ref: JS Array find() function
參考: JS Array find()函數(shù)
Return value: A first element which passes the test.
返回值:通過測試的第一個元素。
Example:
例:
//function for positive valuefunction getPositive(n){return n>=0;}Input:var numbers = [-30, -10, 20, 20, 30, 0, 40];Function call:var item = numbers.find(getPositive);Output:20JavaScript code to find first positive number from an array using Array.find() method
JavaScript代碼使用Array.find()方法從數(shù)組中查找第一個正數(shù)
<html> <head> <title>JavaScipt Example</title> </head><body><script>//function for positive valuefunction getPositive(n){return n>=0;}var numbers = [-30, -10, 20, 20, 30, 0, 40];//find first positive numbervar 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 命令示例
總結
以上是生活随笔為你收集整理的find 命令示例_数组find()方法以及JavaScript中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 打印列表元素_Python
- 下一篇: jsonp请求html页面,JavaSc