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!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment