Home Web Front-end JS Tutorial Summary of usage examples of array processing functions of basic JavaScript functions

Summary of usage examples of array processing functions of basic JavaScript functions

Jul 25, 2017 pm 03:49 PM
javascript js deal with

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 = [&#39;item 1&#39;, &#39;item 2&#39;, &#39;item 3&#39;];
       var list = &#39;<ul><li>&#39; + arr.join(&#39;</li><li>&#39;) + &#39;</li></ul>&#39;;
 </script>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

Output result:
George,John,Thomas
George
John,Thomas


##sort()

Sort the elements of the 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:

<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>
Copy after login

Output result:
John,George,Thomas
George,John,Thomas

Let’s look at another example:

<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>
Copy after login

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 you want to sort by numerical size, you need to change the default sorting method. , specify the sorting rules yourself.

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>
Copy after login
Output results:

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

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.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

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

A quick guide to CSV file manipulation A quick guide to CSV file manipulation Dec 26, 2023 pm 02:23 PM

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,

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

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

How to solve the problem after the upgrade from win7 to win10 fails? How to solve the problem after the upgrade from win7 to win10 fails? Dec 26, 2023 pm 07:49 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

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 The relationship between js and vue Mar 11, 2024 pm 05:21 PM

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.

See all articles