Home  >  Article  >  Web Front-end  >  Introduction to array operations in JavaScript_javascript skills

Introduction to array operations in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:23:261039browse

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:
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