In javascript, the find() method is used to get the first element in the array that meets the conditions. This method will call a callback function for each element in the array, and test whether the array element meets the conditions in the callback function. When the element in the array meets the conditions, the element will be returned, and the subsequent value will not call the callback function again. .
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The find() method returns the value of the first element in the array that satisfies the provided test function.
The find() method calls a callback function execution for each element in the array:
When the element in the array returns true when testing the condition, find () returns elements that meet the conditions, and subsequent values will not call the execution function.
If there is no element that meets the conditions, undefined is returned
Note: The find() function will not be executed for an empty array.
Note: find() does not change the original value of the array.
Syntax: array.find(callback, thisArg)
Description | |
---|---|
callback | Function that is run for each element in the array. Function parameters:
|
thisValue | 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 |
var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18;} function myFunction() { document.getElementById("demo").innerHTML = ages.find(checkAdult);}
18
javascript advanced tutorial]
The above is the detailed content of What does the javascript find() method do?. For more information, please follow other related articles on the PHP Chinese website!