Home > Web Front-end > JS Tutorial > What does the javascript find() method do?

What does the javascript find() method do?

青灯夜游
Release: 2021-10-20 16:43:02
Original
7371 people have browsed it

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. .

What does the javascript find() method do?

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)

##ParametersDescription Optional. The value passed to the function usually uses the "this" value.
callback Function that is run for each element in the array.

Function parameters:

  • element (required) - the current element being processed in the array

  • index (optional) ) - the index of the current element being processed in the array

  • array (optional) - the array object to which the current element belongs

thisValue If this parameter is empty, "undefined" will be passed to the "this" value
Return value: Returns the first array element value that meets the test conditions, if If no condition is met, undefined is returned.

Example: Get the first element in the array whose age is greater than 18

var ages = [3, 10, 18, 20]; 
function checkAdult(age) {
    return age >= 18;}
 function myFunction() {
    document.getElementById("demo").innerHTML = ages.find(checkAdult);}
Copy after login

Output result:

18
Copy after login

[Recommended learning:

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template