


1. concat() connects two or more arrays
This method will not change the existing array, but will only return a copy of the connected array.
For example:
1 <script type="text/javascript"> 2 var arr = [1, 2, 3]; 3 var arr1 = [11, 22, 33]; 4 document.write(arr.concat(4, 5, arr1)); 5 </script>
Output result:
1,2,3,4,5,11,22,33
2, join()
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:
1 <script type="text/javascript"> 2 var arr = ['item 1', 'item 2', 'item 3']; 3 var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; 4 </script>
list result:
'
- item 1
- item 2 < ;li>item 3
This is by far the fastest method! Using native code (such as join()), regardless of what the system does internally, is usually much faster than non-native. ——James Padolsey, james.padolsey.com
3. pop() Delete and return the last element of the array
The pop() method will delete the last element of the array An element, decrements the array length by 1, and returns the value of the element it deletes.
If the array is already empty, pop() does not change the array and returns an undefined value
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.pop() + "<br/>"); 5 document.write(arr); 6 </script>
Output result:
George,John,Thomas Thomas George,John
4 , push() adds one or more elements to the end of the array and returns the new length
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.push("James") + "<br/>"); 5 document.write(arr); 6 </script>
Output result:
George,John,Thomas 4 George,John,Thomas,James
5, unshift() adds one or more elements to the beginning of the array and returns the new length
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.unshift("James") + "<br/>"); 5 document.write(arr); 6 </script>
Output result:
George,John,Thomas 4 James,George,John,Thomas
6 , reverse() reverses the order of elements in the array
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.reverse()); 5 </script>
Output result:
George,John,Thomas Thomas,John,George
7. shift() deletes and returns the first element of the array
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.shift() + "<br/>"); 5 document.write(arr); 6 </script>
Output result:
George,John,Thomas George John,Thomas
8. slice(start,end) Returns the selected element from an existing array
Please note that this method does not modify the array, but returns a subarray
For example:
1 <script type="text/javascript"> 2 var arr = ["George", "John", "Thomas"]; 3 document.write(arr + "<br/>"); 4 document.write(arr.slice(1) + "<br/>"); // 从第一个元素开始截取到 数组结尾 5 document.write(arr); 6 </script>
Output result:
George,John,Thomas John,Thomas George,John,Thomas
9, sort() Sorts the elements of an array
Reference to the array. Please note that the array is sorted on the original array and no copy is generated
This method defaults to sorting in the order of character encoding (ASCII)
For example:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "John"; 4 arr[1] = "George"; 5 arr[2] = "Thomas"; 6 document.write(arr + "<br/>"); 7 document.write(arr.sort()); 8 </script>
Output Result:
John,George,Thomas George,John,Thomas
Let’s look at another example:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort()); 11 </script>
Output result:
10,5,40,25,1000,1 1,10,1000,25,40,5
We can see that it is not sorted by numerical size as we think. If we want to sort by To sort by numerical size, you need to change the default sorting method and specify the sorting rules yourself.
As follows:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = 10 4 arr[1] = 5 5 arr[2] = 40 6 arr[3] = 25 7 arr[4] = 1000 8 arr[5] = 1 9 document.write(arr + "<br/>"); 10 document.write(arr.sort(function (a, b) {return a - b;}));// 从大到小 11 </script>
Output result:
10,5,40,25,1000,1 1,5,10,25,40,1000
What if you want to sort in descending order?
Change the sorting rule to:
function (a, b) {return b – a;}
That’s OK
10, splice( ) Delete elements and add new elements to the array
The splice() method has different functions from the slice() method. The splice() method will directly modify the array
(1) Delete the array elements in the specified range:
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 3); // 删除第三个元素以后的三个数组元素(包含第三个元素) 12 document.write(arr); 13 </script>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,Martin
(2) Insert the specified element starting from the specified subscript (the number of elements is not limited):
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2, 0, "William","JACK"); // 在第三个元素之前插入"William","JACK" 12 document.write(arr); 13 </script>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,William,JACK,Thomas,James,Adrew,Martin
(3) Delete the array elements in the specified range and replace them with the specified elements (the number of elements is not limited):
1 <script type="text/javascript"> 2 var arr = new Array(6); 3 arr[0] = "George"; 4 arr[1] = "John"; 5 arr[2] = "Thomas"; 6 arr[3] = "James"; 7 arr[4] = "Adrew"; 8 arr[5] = "Martin"; 9 10 document.write(arr + "<br/>"); 11 arr.splice(2,3,"William","JACK"); // 删除第三个元素以后的三个数组元素(包含第三个元素),并用"William","JACK"进行替换 12 document.write(arr); 13 </script>
Output result:
George,John,Thomas,James,Adrew,Martin George,John,William,JACK,Martin
The above is the detailed content of Detailed sample code explaining JavaScript array operation function methods. For more information, please follow other related articles on the PHP Chinese website!

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor

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