Summary of common JavaScript Array methods and javascript Array
In JavaScript, We need to operate arrays from time to time. The common methods are summarized as follows:
1. Add data
Adding data to arrays in JavaScript is mainly divided into two methods.
Add content from the end of the array: push method
Add content from the front end of the array: unshift Method
The returned values of these two methods are the length of the array.
Var arr = [1, 2, 3]; // Add arr from the end. push (4); console. log (arr); // [1, 2, 4] // Add arr from the front end. unshift (0); console. log (arr); // [0, 1, 2, 3, 4]
2. delete data
Like adding data, you can delete data in two ways.
Add content from the end of the array: pop Method
Add content from the front end of the array: shift Method
The returned values of both methods are deleted data.
Var arr = [1, 2, 3]; // Delete arr from the end. pop (); console. log (arr); // [1, 2] // Delete arr from the front end. unshift (); console. log (arr); // [2]
3. Delete and add data from a specified location
Splice (startIndex, deleteCount, addValue1, addvalue2 ...): delete deleteCount data starting from the startIndex position of the array, and insert addValue1, addValue2, etc. The returned value is an array composed of the deleted array.
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 above, the array deletion method returns the deleted content, and the added method returns the length after the array is changed.
4. Reverse Order Array
Reverse (): sorts the array content in reverse order.
var arr=[1,2,3]; arr.reverse(); console.log(arr);//[3,2,1]
5. Combine the array content into a string with a specific Separator
Join (seperator): A string consisting of the array content separated by seperator
var arr=[1,2,3]; arr.join(','); console.log(arr);//1,2,3
6. merge two Arrays
Concat (): merges the array and returns a 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, the array is sorted in ascending order by numbers or letters, but you can also customize the descending order.
Var arr = [3, 5, 1]; arr. sort (); console. log (arr); // [, 5]; // specify the descending order var arr1 = [, 7]; arr1.sort (function (a, B) {return B-a;}) console. log (arr1); // [7, 4, 2]
8. truncate sub-array
Slice (startIndex, endIndex): intercept the content from startIndex to endIndex in the array, excluding the content of the endIndex location 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 location of the first element in the array where data is located. If no value is found,-1 is returned.
var arr=[1,2,3]; var loc=arr.indexOf(1); console.log(loc);//0 var newLoc=arr.indexOf(4); console.log(newLoc);//-1
10. iterator
Arrays have many iterator methods.
A. Normal traversal array: forEach (function (value, index, arr) {}), where index is index, value is value, and arr is the array itself
Var arr = [, 3]; // index is the index and value is the value arr. forEach (function (value, index, arr) {console. log (''index:" + index + "" + "value:" + value )})
B. filter Array: filter (): filters the Array Based on the filter conditions, but does not change the original array.
Var arr = [1, 2, 3, 4]; // return the content larger than 2 in the array var newArr = arr. filter (function (item) {return item> 2;}) console. log (arr); // [1, 2, 3, 4] console. log (newArr); // [3, 4]
C. ing array: map (). After each element of the array is mapped, a new array is returned without changing the original array.
Var arr = [1, 2, 4]; // doubles each value of the array var newArr = arr. map (function (item) {return item * 2;}); console. log (arr); // [1, 2, 3, 4] console. log (newArr); // [2, 4, 6, 8]
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.