Home Web Front-end JS Tutorial Introduction to array operations in JavaScript_javascript skills

Introduction to array operations in JavaScript_javascript skills

May 16, 2016 pm 04:23 PM
javascript Array operations

The array object in JavaScript comes with some methods, and you can use these methods to operate on the array.

join()

You can use the join() method to combine members of an array into a string:


Copy code The code is as follows:

var o = [1,2,3];
console.log(o.join());//1,2,3
console.log(o.join(" "));//1 2 3
var emptyArray = new Array(10);
console.log(emptyArray.join("-"));//----------


As you can see from the above example, if the join() method takes no parameters, JavaScript will use commas as delimiters to merge all members into a string; if the join() method accepts parameters, then this parameter will be used as separator to use.

reverse()

You can use the reverse() method to reverse the order of members in the array:


Copy code The code is as follows:

//reverse()
o.reverse();
console.log(o);//[3,2,1]


As you can see, after calling the reverse() statement, the array itself will change.

The return result of executing the reverse() statement is the changed array object.

sort()

You can use the sort() method to sort the members in the array (alphabetical order by default). Like the reverse() statement, the sort() statement modifies the array itself and returns the modified array object:


Copy code The code is as follows:

var a = ["Phone", "Mobile",,,"Canon"];
a.sort();
console.log(a);//["Canon", "Mobile", "Phone", undefined, undefined]
var b = [33,44,111];
console.log(b.sort());//[111, 33, 44]
console.log(b.sort(function(a,b){return a-b}));//[33, 44, 111]


As you can see, the sort() statement also accepts a function as a parameter to implement custom sorting.

concat()

You can use the concat() method to concatenate arrays:


Copy code The code is as follows:

var c = [1,2,3];
console.log(c.concat(4));//[1, 2, 3, 4]
console.log(c.concat(5,6));//[1, 2, 3, 5, 6]
console.log(c.concat([7,8]));//[1, 2, 3, 7, 8]
console.log(c.concat([9,10], [11,12]));//[1, 2, 3, 9, 10, 11, 12]
console.log(c.concat([42,43,[44,45]]));//[1, 2, 3, 42, 43, [44, 45]]
console.log(c);//[1, 2, 3]


As you can see, unlike reverse() and sort(), the concat() statement only returns the spliced ​​result and does not cause any modification to the array itself.

slice()

You can use the slice() statement to get the sub-array in the array:

Copy code The code is as follows:

var d = [1,2,3,4,5,6];
console.log(d.slice(0,3));//[1,2,3]
console.log(d.slice(3,1));//[]

Like concat(), the slice() statement only returns the result of the operation and does not cause any modification to the array itself. For the two parameters in the slice() statement, JavaScript follows the principle of "include before and exclude after": the array member specified by the first parameter will appear in the subarray, while the array member specified by the second parameter will appear in the subarray. Will not appear.

splice()

You can use the splice() statement to perform insertion and deletion operations on the array. The first parameter specifies the position of insertion or knockout (positional member), the second parameter specifies the number of knockout members (starting to knockout from the positional member), starting from the third parameter, all parameters will be inserted into In the array (insert from before position member). The result returned by the splice() statement is an array composed of the deleted array members. Unlike concat() and slice(), splice() will modify the array itself.


Copy code The code is as follows:

var e = [1,2,3,4,5,6,7];
console.log(e.splice(1,2));//[2,3]
console.log(e);//[1,4,5,6,7]
console.log(e.length);//5
e.splice(1,0,2,3,[4,5]);
console.log(e);//[1, 2, 3, [4, 5], 4, 5, 6, 7]
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 Article Tags

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

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

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

See all articles