Home > Web Front-end > JS Tutorial > What is the usage of javascript find()

What is the usage of javascript find()

青灯夜游
Release: 2021-11-24 17:41:08
Original
13379 people have browsed it

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

What is the usage of javascript find()

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)
Copy after login
ParametersDescription
function(currentValue, index,arr) Required. The function that needs to be executed for each element of the array.
  • currentValue Required. Current element

  • #index Optional. The index value of the current element

  • #arr 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

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

What is the usage of javascript find()

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

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