Home > Web Front-end > JS Tutorial > body text

What is the use of the javascript find() method?

青灯夜游
Release: 2023-01-05 16:11:16
Original
4201 people have browsed it

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

What is the use of the javascript find() method?

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)
Copy after login
ParametersDescription
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
Copy after login

[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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!