javascript find方法查找元素
javascript find 概述
find()
方法返回数组中满足测试条件的一个元素,如果没有满足条件的元素,则返回 undefined
。
区别:findIndex()
方法返回的是满足条件的元素的索引,而非它的值。
javascript find 语法
<code class="javascript">arr.find(callback[, thisArg])</code>
参数
callback
在数组每一项上执行的函数,接收 3 个参数:
-
element
当前遍历到的元素。 -
index
当前遍历到的索引。 -
array
数组本身。
thisArg
可选,指定 callback 的 this 参数。
javascript find 描述
find方法对数组中的每一项元素执行一次callback
函数,直至有一个callback返回true。当找到了这样一个元素后,该方法会立即返回这个元素的值,否则返回undefined。注意callback函数只会在分配了值的数组索引上调用,而不会在已删除或未分配值的索引上调用。
调用callback函数带有3个参数:当前元素的值、当前元素的索引,以及数组本身。
如果提供了thisArg参数,那么它将作为每次callback函数执行时的上下文对象,否则上下文对象为undefined。
find方法不会改变数组。
在第一次调用callback函数时会确定元素的索引范围,因此在find方法开始执行之后添加到数组的新元素将不会被callback函数访问到。如果数组中一个尚未被callback函数访问到的元素的值被callback函数所改变,那么当callback函数访问到它时,它的值是将是根据它在数组中的索引所访问到的当前值。被删除的元素不会被访问到。
javascript find 实例
寻找数组中的质数
下面的例子展示了如何从一个数组中寻找质数(如果找不到质数则返回undefined)
<code class="javascript">function isPrime(element, index, array) { var start = 2; while (start 1); } console.log( [4, 6, 8, 12].find(isPrime) ); // undefined, not found console.log( [4, 5, 8, 12].find(isPrime) ); // 5</code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Hard drive serial numbers and MAC addresses are important identifiers in computer hardware and are very useful in managing and maintaining computer systems. This article will introduce how to find the hard disk serial number and MAC address. 1. Find the hard drive serial number. The hard drive serial number is a unique identifier used by the hard drive manufacturer to identify and track the hard drive. In different operating systems, the method of finding the hard drive serial number is slightly different. Windows: Open Command Prompt (search for "cmd" in the Start menu) and enter the following command and press Enter: wmicdisk

Apple's Find My app allows you to locate your iPhone or other device to prevent it from being lost or forgotten. While Find My is a useful tool for tracking devices, you may want to disable it if you're concerned about privacy issues, don't want to drain your battery, or for other reasons. Fortunately, there are several ways to turn off Find My on iPhone, all of which we will explain in this article. How to Turn off Find My on iPhone [4 Methods] You can turn off Find My on iPhone in four ways. If you used Method 1 to turn off Find, you can do this from the device you want to disable it on. To proceed with methods 2, 3, and 4, the iPhone that you want to turn off Find Finder should be powered off or

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to Check the Serial Number of a Computer Hard Drive With the development of computer technology, computer hard drives have become an indispensable part of our lives. Whether it is storing important files or installing operating systems and software, you need to rely on the hard disk to complete it. Understanding some basic information about the computer hard drive, such as the hard drive's serial number, can help us better manage and maintain the computer system. So, how to check the serial number of a computer hard drive? This article will introduce several common methods. Method 1: Use the command line tool that comes with Windows system Windows system
