Commonly used array methods in JavaScript
some() method
This method checks whether at least one element of the array meets the conditions checked by the parameter function.
<script> // JavaScript to illustrate // lastIndexOf() method function isGreaterThan5(element, index, array) { return element > 5; } function func() { // Original array var array = [2, 5, 8, 1, 4]; // Checking for condition in array var value = array.some(isGreaterThan5); document.write(value); } func(); </script>
Output:
true
reduce() method
The array reduce() method in JavaScript is used to reduce an array into a single value and for each value of the array (from left to right) and the return value executes a provided function. The function is stored in the accumulator.
<script> // Original array var numbers = [88, 50, 25, 10]; // Performing reduce method var sub = numbers.reduce(geeks); function geeks(total, num) { return total - num; } document.write(sub) </script>
Output:
3
map() method
The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array . This is a non-mutating method. Typically, the map() method is used to iterate over an array and call a function on each element of the array.
<script> // Original array var numbers = [4, 9, 16, 25]; // Performing map method var sub = numbers.map(geeks); function geeks() { return numbers.map(Math.sqrt); } document.write(sub)
Output:
2 3 4 5
every() method
This method checks whether all elements of the array satisfy the given condition, which is provided by the function passed to it as a parameter .
<script> // JavaScript code for every() function function ispositive(element, index, array) { return element > 0; } function func() { var arr = [ 11, 89, 23, 7, 98 ]; // Check for positive number var value = arr.every(ispositive); document.write(value); } func(); </script>
Output:
true
flat() method
This method creates a new array containing multiple arrays. Basically create a simple array from an array containing multiple arrays.
<script> //Original array var arr = [ [11, 89], [23, 7], 98 ]; // Performing flat method var geeks = arr.flat(); document.write(geeks) </script>
Output:
11,89,23,7,98
flatMap() method
This method is used to flatten the input array elements into a new array. This method first maps each element with the help of map function and then flattens the input array elements into a new array.
<script>const myAwesomeArray = [[1], [2], [3], [4], [5]]var geeks = myAwesomeArray.flatMap(arr => arr * 10) console.log(geeks);
Output:
10、20、30、40、50
filter() method
This method is used to create a new array from the given array consisting only of the parameters in the given array that satisfy the function Consists of those elements that set conditions.
<script> function isPositive(value) { return value > 0; } function func() { var filtered = [112, 52, 0, -1, 944] .filter(isPositive); document.write(filtered); } func(); </script>
Output:
112、52、944
findindex() method
This method returns the index of the first element in the given array that satisfies the provided test function. Otherwise, -1 is returned.
<script> var array = [ 10, 20, 30, 110, 60 ]; function finding_index(element) { return element > 25; } document.write(array.findIndex(finding_index)); </script>
Output:
2
find()方法
此方法用于获取满足所提供条件的数组中第一个元素的值。它检查数组的所有元素,以及第一个满足条件的要打印的元素。
<script> // Input array contain some elements. var array = [10, 20, 30, 40, 50]; // Function (return element > 10). var found = array.find(function(element) { return element > 20; }); // Printing desired values. document.write(found); </script>
输出:
30
fill()方法
此方法用于使用给定的静态值填充数组。该值可以用于填充整个数组,也可以用于填充数组的一部分。
<script> // JavaScript code for fill() function function func() { var arr = [1, 23, 46, 58]; // Here value = 87, start index = 1 and // and last index = 3 arr.fill(87, 1, 3); document.write(arr); } func(); </script>
输出:
1,87,87,58
forEach()方法
该方法为数组的每个元素调用一次提供的函数。提供的函数可以对给定数组的元素执行任何类型的操作。
<script> function func() { // Original array const items = [1, 29, 47]; const copy = []; items.forEach(function(item){ copy.push(item*item); }); document.write(copy); } func(); <script>
输出:
1,841,2209
sort()方法
此方法用于对数组进行排序。数组可以是任何类型,例如字符串,数字,字符等。
<script> // Original array var numbers = [88, 50, 25, 10]; // Performing sort method var sub = numbers.sort(geeks); function geeks(a, b) { return a - b; } document.write(sub) </script>
输出:
10、25、50、88
concat()方法
此方法用于将两个或多个数组合并在一起。此函数不会更改作为参数传递的原始数组。
<script> // JavaScript code for concat() function function func() { var num1 = [11, 12, 13], num2 = [14, 15, 16], num3 = [17, 18, 19]; document.write(num1.concat(num2, num3)); } func(); </script>
输出:
11,12,13,14,15,15,16,17,18,19
include()方法
此方法用于知道数组中是否存在特定元素,因此,它返回true或false,即,如果该元素存在,则返回true,否则返回false。
<script> // Taking input as an array A // having some elements. var A = [ 1, 2, 3, 4, 5 ]; // Include() function is called to // test whether the searching element // is present in given array or not. a = A.includes(2) // Printing result of function. document.write(a); </script>
输出:
true
reverse()方法
此方法用于数组的就地反转。数组的第一个元素变为最后一个元素,反之亦然。
<script> function func() { //Original Array var arr = [34, 234, 567, 4]; document.write("Original array: " + arr); //Reversed array var new_arr = arr.reverse(); document.write("<br>Newly reversed array: "); document.write(new_arr); } func(); <script>
输出:
原始数组:34、234、567、4新反向阵列:4、567、234、34
推荐教程:《JS教程》
The above is the detailed content of Commonly used array methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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 JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
