[igeek manual] common methods of JS array operation

What the igeek manual shares with you today is: Common methods of JS array operation , I hope you can gain something after reading it~

 


 

1. push adds the last item

Adds an item to the end of the array and returns the length of the array. You can add any type of value as an item of the array.

var arr = [ 1 , 2 ];
arr.push(6)     // arr: [1,2,6]
arr.push('aa')  // arr: [1,2,6,"aa"]
arr.push(undefined)  // arr: [1,2,6,"aa",undefined]
arr.push({a: "A", b: "B"})  // [1,2,6,"aa",undefined,{a: "A", b: "B"}]
2. Unshift adds an item to the front
var arr = [1,2];
arr.unshift(9)      // [9, 1, 2]
arr.unshift('aa')   // ['aa',9, 1, 2]
3. pop deletes the last item

Removes the last item and returns the value of the removed element; returns undefined if the array is empty. operate on the array itself

var arr = [ 1 , 2 , 3 , 4 , 5 ];
arr.pop()       // arr: [1, 2, 3, 4]
arr.pop()       // arr: [1, 2, 3]
4. shift delete the first item
var arr = [ 1 , 2 , 3 , 4 , 5 ];
arr.shift()     // [2, 3, 4, 5]
arr.shift()     // [3, 4, 5]
5. Slice intercepts (slices) the array to get the intercepted array

Do not change the original array, get a new array

slice(start,end)

var arr = [ 1 , 2 , 3 , 4 , 5 ];
var a = arr.slice(1)        // a: [2,3,4,5]
var a = arr.slice(1,3)      // a: [2,3]
var a = arr.slice(3,4)      // a: [5]
6, splice splicing array

By changing the original array, you can delete before shift, delete after pop, and increase before unshift, which has the same effect as adding after push. index starts at 0

splice(index,howmany,item1,.....,itemX)

var arr = [ 1 , 2 , 3 , 4 , 5 ];

push: arr.splice(arr.length, 0, 6)  //  [1, 2, 3, 4, 5, 6]
unshift: arr.splice(0, 0, 6)        // [6, 1, 2, 3, 4, 5]
pop: arr.splice(arr.length-1, 1)    // [1, 2, 3, 4]
shift: arr.splice(0, 1)             // [2, 3, 4, 5]

arr.splice(1)   // [1]
arr.splice(1, 2)    // [1, 4, 5]
arr.splice(1, 0, 'A')   // [1, "A",2,3, 4, 5]
arr.splice(1, 2, 'A', 'B')   // [1, "A", "B", 4, 5]
7, concat array merge

After merging, a new array is obtained, the original array is not changed

var arr1 = [ 1 , 2 ];
var arr2 = [ 3 , 4 , 5 ];
var arr = arr1.concat (arr2)      // [1,2,3,4,5]
8. indexOf array element index

And return the element index, if there is no return -1, the index starts from 0

var arr = ['a','b','c','d','e']; 
arr.indexOf('a');       //0
arr.indexOf(a);         //-1
arr.indexOf('f');       //-1
arr.indexOf('e');       //4
9. Join array to string
var a, b;
a = [0, 1, 2, 3, 4];
b = a.join("-");    // 0-1-2-3-4
10, reverse array flip

And return the flipped original array, the original array flipped

var a = [ 1 , 2 , 3 , 4 , 5 ];
a.reverse() //a: [5, 4, 3, 2, 1] returns [5, 4, 3, 2, 1]

 

 


 

Today's sharing of the igeek manual is here, here is igeekbar , I'm the geek killer of iGeekBar~ Do you have any questions and comments! Welcome to leave a message to hook me up~~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326526709&siteId=291194637