


Summary of usage examples of array processing functions of basic JavaScript functions
join()
Put all elements of the array into a string. Elements are separated by the specified delimiter.
For example:
<script type="text/javascript"> var arr = ['item 1', 'item 2', 'item 3']; var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; </script>
list result:
'
- item 1
- item 2
- 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
pop()
Delete and return the last element of the array
The pop() method will delete the last element of the array and return the array to Decrements the length by 1 and returns the value of the element it removed.
If the array is already empty, pop() does not change the array and returns an undefined value
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.pop() + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
Thomas
George,John
push()
Add one or more elements to the end of the array and return the new length
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.push("James") + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
4
George,John,Thomas,James
unshift()
Towards the beginning of the array Adds one or more elements and returns the new length
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.unshift("James") + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
4
James, George, John, Thomas
shift()
Delete and return the first element of the array
For example:
<script type="text/javascript"> var arr = ["George", "John", "Thomas"]; document.write(arr + "<br/>"); document.write(arr.shift() + "<br/>"); document.write(arr); </script>
Output result:
George,John,Thomas
George
John,Thomas
##sort()
Sort the elements of the array
This method defaults to sorting in the order of character encoding (ASCII)
For example:
<script type="text/javascript"> var arr = new Array(6); arr[0] = "John"; arr[1] = "George"; arr[2] = "Thomas"; document.write(arr + "<br/>"); document.write(arr.sort()); </script>
Output result:
John,George,Thomas
George,John,Thomas
<script type="text/javascript"> var arr = new Array(6); arr[0] = 10 arr[1] = 5 arr[2] = 40 arr[3] = 25 arr[4] = 1000 arr[5] = 1 document.write(arr + "<br/>"); document.write(arr.sort()); </script>
10,5,40,25,1000,1
1,10,1000,25,40,5
is as follows:
<script type="text/javascript"> var arr = new Array(6); arr[0] = 10 arr[1] = 5 arr[2] = 40 arr[3] = 25 arr[4] = 1000 arr[5] = 1 document.write(arr + "<br/>"); document.write(arr.sort(function (a, b) {return a - b;}));// 从大到小 </script>
10,5,40,25,1000,1
1,5,10,25,40,1000
If you want descending order What about the arrangement?
Change the sorting rule to:
function (a, b) {return b - a;}
That’s OK
The above is the detailed content of Summary of usage examples of array processing functions of basic JavaScript functions. 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



1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

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

Quickly learn how to open and process CSV format files. With the continuous development of data analysis and processing, CSV format has become one of the widely used file formats. A CSV file is a simple and easy-to-read text file with different data fields separated by commas. Whether in academic research, business analysis or data processing, we often encounter situations where we need to open and process CSV files. The following guide will show you how to quickly learn to open and process CSV format files. Step 1: Understand the CSV file format First,

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

If the operating system we use is win7, some friends may fail to upgrade from win7 to win10 when upgrading. The editor thinks we can try upgrading again to see if it can solve the problem. Let’s take a look at what the editor did for details~ What to do if win7 fails to upgrade to win10. Method 1: 1. It is recommended to download a driver first to evaluate whether your computer can be upgraded to Win10. 2. Then use the driver test after upgrading. Check if there are any driver abnormalities, and then fix them with one click. Method 2: 1. Delete all files under C:\Windows\SoftwareDistribution\Download. 2.win+R run "wuauclt.e

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

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

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
