Home  >  Article  >  Web Front-end  >  The shortest and easiest to understand method of js Array operation_javascript skills

The shortest and easiest to understand method of js Array operation_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:07921browse

Methods of Array
1 Array.join(): Concatenate all elements into a string using symbols and return it. If the element is not a basic type, call toString.
It corresponds to string.split();
arr = [1,2,true,3,4,5];
(arr.join('-') == '1-2-true-3 -4-5';

2 Array.reverse(): Arrange array in reverse order
arr = [1,2,true,3,4,5];
arr.reverse();// arr == [5,4,3,true,2,1];

3 Array.sort(): Sorting, you can pass a sorting function as parameter
arr.sort(function(a,b){
return a-b;
}) ;

4 Array.concat(): splicing function,
splices new elements at the end and returns the spliced ​​array, but does not change the original array; the parameter can be one element or multiple elements , an array,
If it is one element or multiple elements, add these elements directly to the end. If it is an array, take out each element of the array and splice them to the end.
a = [1,2,3];
a.concat(4,5)// return [1,2,3,4,5]
a.concat([4,5] )// return [1,2,3,4,5]
a.concat([4,5],[6,7]);//return [1,2,3,4,5,6 ,7]
a.concat([4,[5,6]])//return [1,2,3,4,[5,6]]//Note

5 Array.slice(startPos, endPos): substring function (the original array remains unchanged)
Start from startPos and end with endPos but do not include the elements on endPos
If there is no endPos, then get to the end
If pos is negative, then the inverse number
a = [1,2,3,4,5];
a.slice(0,3)// return [1,2,3]
a.slice(3)//return [4,5]
a.slice(1,-1)//return [2,3,4]//from Start fetching from the first one, and fetch the first one from the last, but excluding the first one from the last
a.slice(1,-2);//return [2,3]//Start from the first one, Get the second to last one, but not including the second to last one

6 Array.splice(startPos, length, [added1, added2...]) Random access function
can randomly delete one (some) elements or add some elements.
If there are only two parameters, delete a total of length elements starting from startPos from the array
If there are more than two parameters, delete a total of length elements starting from startPos from the array, and then start from the position just deleted Add the following element
If the added element is an array, treat the array as an element (difference from concat)
a = [1,2,3,4,5];
a. splice(1,2)//return [2,3]; a==[1,4,5]
a.splice(1,2,6,7,8)//return [2,3] ; a==[1,6,7,8,4,5]
a.splice(1,2,[6,7,8]);//return [2,3]; a==[ 1,[6,7,8],4,5]

7 Array.push() and Array.pop();
both operate on the last element, push is to add, pop is to delete the last element and return the element

8 Array.unshift() and Array.shift()
both operate on the first element, unshift is to add, shift is to delete the first element and return the element

Total
These methods will change the original array: reverse, sort, splice, push, pop, unshift, shift
These methods will not change the original array :join, concat, splice

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