Home >Web Front-end >JS Tutorial >What is the use of the javascript find() method?
The find() method in js is used to return the value of the first element of the array that passes the test (judged within the function). If there is no element that meets the conditions, "undefined" is returned; the basic syntax is "array.find( function(current element, index, array object))".
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 of the array that passes the test (judged within the function).
The find() method calls a function execution once for each element in the array:
When the elements in the array return 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, it returns undefined
find() For an empty array, the function will not be executed.
find() does not change the original value of the array.
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. Function parameters: Parameter description currentValue is required. The current element index is optional. The index value arr of the current element is optional. The array object to which the current element belongs |
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 |
Return value: Returns the first array element value that meets the test condition, if If no condition is met, undefined is returned.
Example:
let test = [1, 2, 3, 4, 5]; let a = test.find(item => item > 3); console.log(a); //4 let b = test.find(item => item == 0); console.log(b); //undefined
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What is the use of the javascript find() method?. For more information, please follow other related articles on the PHP Chinese website!