Summarize all kinds of operations of Javascript on array objects

  • 2021-07-13 04:06:49
  • OfStack

Array

Array Definition: Simply put, an ordered set of data whose index is a naturally growing integer starting from 0 and whose element value can be any js data! And contains an attribute named length, which represents the number of array elements!

1. Define arrays in three ways:

Mode 1:


var arr=new Array(); 
arr[0]= " 11 " ; 
arr[1]= " 22 " ; 
arr[2]= " 33 " ; 

Mode 2:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 

Mode 3:


var arr=[ " 11 " , " 22 " , " 33 " ]; 

2. The length attribute of the array can get the length of the array, and can also truncate and empty the array. If the set value is smaller than its current value, the array will be truncated and the elements at its tail will be lost.

If you set a value larger than its current value, the length of the array increases, and new elements are added to the end of the array with a value of undefined:


var arr=[ " 11 " , " 22 " , " 33 " ]; 
arr.length //3 Returns the length of the array  
arr.length = 2 //['11','22'] Before truncating the array 2 A  
arr.length = 5 //['11','22',undefined,undefined,undefined] Automatically add undefined 
arr.length = 0 //[] Empty the array  

3. Traverse the array:


var arr=[ " 11 " , " 22 " , " 33 " ]; 
//for Cycle  
for(var i=0;i<arr.length;i++){ 
 console.log(i) 
} 
//for in Cycle  
for( i in arr ){ 
 console.log(arr[i]) 
} 
//forEach Cycle  
arr.forEach(function(i){ 
 console.log(i) 
}) 

4. Adding and deleting arrays:

push() Adds one or more elements to the end of the array, and the return value is the length of the array after adding elements.


var arr=[1,2,3]; 
arr.push(4) 
console.log(arr) //[1,2,3,4] 
arr.push(5,6,7) 
console.log(arr) //[1,2,3,4,5,6,7] 

unshift() Adds one or more elements to the beginning of the array, and the return value is the length of the array after adding elements.


var arr=[1,2,3]; 
arr.unshift(4) 
console.log(arr) //[4,1,2,3] 
arr.unshift(5,6,7) 
console.log(arr) //[5,6,7,1,2,3,4] 

pop() Delete from the end of the array, and the return value is the value of the deleted element.


var arr=[1,2,3]; 
arr.pop() 
console.log(arr) //[1,2] 

shift() Delete from the beginning of the array, and the return value is the value of the deleted element.


var arr=[1,2,3]; 
arr.shift() 
console.log(arr) //[2,3] 

5. join () delimits the array with a specified delimiter and returns a value of type string, without changing the original array:


var arr=[1,2,3,4]; 
arr.join( ' - ' ) // " 1-2-3-4 "  
arr.join( " ) // " 1234 "  
arr.join( '   ' ) // " 1 2 3 4 "  

6. sort () Array Sort:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
0

7. reverse () reverses the array:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
1

8. Get the largest and smallest numbers in the array:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
2

9. slice () returns selected elements from an existing array without changing the original array

1 parameter, starting at the start subscript and ending.

There are two parameters, from the start subscript to the array element at the end subscript (excluding this element):


var arr=[2,8,3,4,12,56]; 
arr.slice(1) //[8, 3, 4, 12, 56] 
arr.slice(1,5) //[8, 3, 4, 12] 

10. splice()

1 parameter, the deletion starts at the subscript of start and ends at the end. Returns the deleted number, which directly modifies the original array.

There are two parameters. Delete the array elements from start subscript to end subscript, and return the deleted number. This directly modifies the original array.

There are three parameters. The elements from start subscript to end subscript are replaced by the third parameter. If the first two numbers are the same, it is a replacement, which directly modifies the original array:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
4

101. concat () can combine two arrays to return a new array:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
5

102. Array deduplication:

Method 1:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
6

Method 2:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
7

Method 3:


function removeRepeat(a){ 
 var arr = []; 
 a.forEach(function(i){ 
   if(arr.indexOf(i) === -1){ 
   arr.push(i); 
  } 
 }); 
 return arr 
} 

Method 4:


var arr=new Array( " 11 " , " 22 " , " 33 " ); 
9

102. prototype property, which directly changes the prototype of the array or adds functionality:


// For example, we add an array 1 The method of summation  
Array.prototype.sum= function(){ 
 var n = 0; 
 this.forEach(function(i){ 
  n+=i; 
 }); 
 return n; 
} 
 
var arr = [1,2,3,4] 
alert(arr.sum()) //10

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: