Summary of Common Methods of JavaScript Array

  • 2021-07-15 03:39:28
  • OfStack

In JavaScript, we need to operate on arrays from time to time. Now we summarize the common methods as follows:

STEP 1 Add data

There are two main ways to add data to arrays in JavaScript.

Add content from the end of array: push method

Adding content from the front end of the array: unshift method

The return value of both methods is the length of the array


var arr=[1,2,3]; 
// Add from the end  
arr.push(4); 
console.log(arr);//[1,2,3,4] 
 
// Add from the front end  
arr.unshift(0); 
console.log(arr);//[0,1,2,3,4] 

Step 2 Delete data

And add data 1 sample, delete data and mainly divided into two ways.

Add content from the end of array: pop method

Adding content from the front end of the array: shift method

The return value of both methods is deleted data


var arr=[1,2,3]; 
// Delete from the end  
arr.pop(); 
console.log(arr);//[1,2] 
 
// Delete from the front end  
arr.unshift(); 
console.log(arr);//[2] 

3. Delete and add data from specified locations

splice (startIndex, deleteCount, addValue1, addValue2...): deleteCount data is deleted from the startIndex position of the array, and then addValue1, addValue2, etc. are inserted, and the return value is an array composed of deleted arrays.


var arr=[1,2,3,4,5]; 
var deleteArr=arr.splice(1,2,8,9); 
 
console.log(deleteArr);//[2,3] 
console.log(arr);//[1,8,9,4,5] 

As we know from the above, the deletion methods of arrays return all the deleted contents, and the addition methods return all the changed lengths of arrays.

4. Inverted arrays

reverse (): Reverses the array contents.


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

5. Compose the contents of the array into a string with a specific delimiter

join (seperator): Compose the contents of the array into a string, and the contents are separated by seperator


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

6. Merge two arrays

concat (): Merges arrays and returns 1 new array without affecting the original array.


var arr1=[1,2,3]; 
var arr2=[4,5,6]; 
 
var newArr=arr1.concat(arr2); 
 
console.log(arr1);//[1,2,3] 
console.log(arr2);//[4,5,6] 
console.log(newArr);//[1,2,3,4,5,6] 

7. Array sorting

sort (): By default, arrays are sorted in ascending numeric or alphanumeric order, but you can customize descending order


var arr=[3,5,1]; 
 
arr.sort(); 
 
console.log(arr);//[1,3,5]; 
 
// Specify descending order  
var arr1=[4,2,7]; 
arr1.sort(function(a,b){ 
  return b-a; 
}) 
console.log(arr1);//[7,4,2] 

8. Intercept a subarray

slice (startIndex, endIndex): Intercept the contents of the array from startIndex to endIndex, excluding the contents of endIndex to form a new array


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

9. Determine the position of the given data in the array

indexOf (data): This method returns the first element position of data in the array, and returns-1 if it is not found


var arr=[1,2,3]; 
 
var loc=arr.indexOf(1); 
console.log(loc);//0 
 
var newLoc=arr.indexOf(4); 
console.log(newLoc);//-1 

10. Iterators

There are many iterator methods for arrays.

a. Common traversal array: forEach (function (value, index, arr) {}), where index is the index, value is the value, and arr is the array itself


var arr=[1,2,3]; 
 
//index Is an index, value Yes value  
arr.forEach(function(value,index,arr){ 
   console.log(''index:"+index+" "+"value:"+value) 
}) 

b. Filter Array: filter (), filters the array according to the filter criteria, but does not change the original array


var arr=[1,2,3]; 
// Delete from the end  
arr.pop(); 
console.log(arr);//[1,2] 
 
// Delete from the front end  
arr.unshift(); 
console.log(arr);//[2] 
0

c. Mapping Array: map (), after each element of the array is mapped by 1, a new array is returned, and the original array will not be changed


var arr=[1,2,3]; 
// Delete from the end  
arr.pop(); 
console.log(arr);//[1,2] 
 
// Delete from the front end  
arr.unshift(); 
console.log(arr);//[2] 
1

Related articles: