Home >Web Front-end >JS Tutorial >What is the usage of javascript find()
In JavaScript, the find() method is used to obtain the first element in the array that meets the specified conditions. This method will call a callback function for each element in the array, and use the callback function to find the element that meets the specified conditions. The first element of the condition; syntax "array.find(function(..),Value)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, the find() method is used to get the first element in the array that meets the specified conditions.
The find() method returns the value of the first element of the array that meets the specified conditions (judged within the function).
The find() method executes a callback function once for each element present in the array:
If an array element for which the function returns a true value is found, find() returns The value of this array element (and does not check the remaining values)
Otherwise returns undefined
Syntax:
array.find(function(currentValue, index, arr),thisValue)
Parameters | Description |
---|---|
function(currentValue, index,arr) | Required. The function that needs to be executed for each element of the array.
|
thisValue | is optional. The value passed to the function usually uses the "this" value. If this parameter is empty, "undefined" will be passed to the "this" value |
Example: Get the first person in the array whose age is greater than or equal to 18 Element
var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } console.log(ages.find(checkAdult));
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What is the usage of javascript find(). For more information, please follow other related articles on the PHP Chinese website!