Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the operation methods of ECMAScript3 array

Shulou Source: shulou.com Published: 2022-06-01 12:45:16 09月26日 Update

This article mainly introduces the ECMAScript3 array operation methods, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Operation method of ECMAScript3 array

Format input array

You can specify that the output is a connection string between elements.

A = [2017, 8, 29]

A.jion ("/"); / / output result: "2017-8-29"

Reverse the order of elements

A = [1, 2, 3]

A.reverse (); / / = > a = [3,2,1]

Array sorting

A = [2017, 8, 29]

A.sort (); / / = > a = [8,29,2017]

You can customize the collation by passing a comparison function.

A = [2017, 8, 29]

A.sort (function (a, b) {

Return b-a; / / descending

}

); / / = > a = [2017, 29, 8]

Array join

A = [1, 2, 3]

A.concat (4,5); / / = > a = [1,2,3,4,5]

A.concat ([6, 9, 10]); / / a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Get array fragments

A = ["A", "B", "C", "D", "E"]

/ / specify the start position and the end position (not included).

A.slice (0,2); / / return ["A", "B"]

/ / specify only the start position

A.slice (3); / / return ["D", "E"]

/ / specify the start position, the end position calculated from the last element

A.slice (- 5,-3); / / return ["A", "B"]

Specify an interval to replace array elements

A = [1, 2, 3, 4, 5]

/ / 2 elements that will start with the second element (index value 1)

/ / replace with the 3 elements following the beginning of the third parameter of splice

B = a.splice (1,2, "A", [100.20], "B")

After the execution of this program, the content of the array an is [1, "A", [10jue 20], "B", 4je 5], and the array b of the return value is [2,3]. If you specify only the first parameter, it is sliced from the source array and returns all elements after the specified position; if you specify the first two parameters, it is equivalent to digging out a portion of the source array.

Add and remove elements at the tail

A = [1, 2, 3]

A.push (4,5); / / = > a = [1,2,3,4,5]

A.pop (); / / = > a = [1,2,3,4]

Insert and delete elements in the header

A = [1, 2, 3]

A.unshift (4, 5, 6); / / > a = [4, 5, 6, 1, 2, 3]

A.shift (); / / = > a = [5,6,1,2,3]

Convert an array to a string

["I", "You", "Who"] .toString (); / / = > "iPertinent who"

Thank you for reading this article carefully. I hope the article "what are the operation methods of ECMAScript3 array" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Array element position article method parameter character string sort output between value interest content function interval can be passed at the same time header Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker Linux NVidia MariaDB