Introduction to array operations in JavaScript_javascript skills
May 16, 2016 pm 04:23 PMThe 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:
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:
//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:
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:
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:
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.
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]

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

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

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

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

Simple JavaScript Tutorial: How to Get HTTP Status Code

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

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way
