Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the javascript find () method

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use the javascript find () method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In javascript, the find () method is used to get the first qualified element in the array. This method will call the callback function once for each element in the array, test whether the array element meets the condition in the callback function, return the element when the element in the array meets the condition, and the subsequent value will not call the callback function.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, 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 for each element in the array to execute:

When the element in the array returns true when the condition is tested, find () returns the element that meets the condition, and the subsequent value does not call the execution function.

Return undefined if there are no eligible elements

Note: find () is not executed for empty arrays.

Note: find () does not change the original value of the array.

Syntax: array.find (callback, thisArg)

Parameter description callback

The function that runs for each element in the array.

Function parameters:

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

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

Array (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" is passed to the "this" value.

Return value: returns the value of the first array element that meets the test condition, or undefined if none meets the condition.

Example: get the first element in the array that is older than 18

Var ages = [3,10,18,20]; function checkAdult (age) {return age > = 18;} function myFunction () {document.getElementById ("demo") [xss_clean] = ages.find (checkAdult);}

Output result:

This is the end of 18 "how to use the javascript find () method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report