Array
Array definition: In short, it is an ordered set of data, indexed as an integer that starts from 0 and grows naturally, and its element value can be any js data! It also contains an attribute called length, which represents the number of array elements!
1. Define an array, there are three ways to define:
Method 1:
var arr=new Array(); arr[0]=“11”; arr[1]=“22”; arr[2]=“33”;
Method 2:
var arr=new Array(“11”,“22”,“33”);
Method 3:
var arr=[“11”,“22”,“33”];
2. The length attribute of the array can obtain the length of the array, or intercept and clear 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 the set value is larger than its current value, the length of the array will increase, and new elements are added to the tail of the array, and their values are undefined:
var arr=[“11″,”22″,”33″]; //3, return the length of the array = 2 //['11','22'], intercept the first 2 arrays = 5 //['11','22',undefined,undefined,undefined], automatically add undefined = 0 //[],Clear the array
3. Iterate over the array:
var arr=[“11”,“22”,“33”]; //For loopfor(var i=0;i<;i++){ (i) } //for in loopfor( i in arr ){ (arr[i]) } //forEach loop(function(i){ (i) })
4. Adding and deleting arrays:
push()
Add one or more elements to the end of the array, and its return value is the length of the array after the element is increased.
var arr=[1,2,3]; (4) (arr) //[1,2,3,4] (5,6,7) (arr) //[1,2,3,4,5,6,7]
unshift()
Add one or more elements to the beginning of the array, and its return value is the length of the array after the element is increased.
var arr=[1,2,3]; (4) (arr) //[4,1,2,3] (5,6,7) (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) //[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) //[2,3]
5. Join() separates the array by the specified delimiter, the return value is string type, and does not change the original array:
var arr=[1,2,3,4]; (‘-‘) //”1-2-3-4″ (”) //”1234″ (‘ ‘) //”1 2 3 4″
6. sort() array sort:
var arr=[2,8,3,4,12,56]; //Sorting from small to large(function(a,b){ return a-b; }); //Sorting from large to small(function(a,b){ return b-a; }); //Random sort(function(a,b){ return () – 0.5 })
7. reverse() reverse() reverse the array:
var arr=[2,8,3,4,12,56]; () //[56, 12, 4, 3, 8, 2]
8. Get the largest and smallest numbers in the array:
var arr = [5, 458 , 120 , -215 , 228 , 400]; var max = (Math, arr); var min = (Math, arr);
9. slice() can return the selected element from the existing array without changing the original array
A parameter that starts from the start subscript to the end.
There are two parameters, the array element from the start subscript to the end subscript (excluding this element):
var arr=[2,8,3,4,12,56]; (1) //[8, 3, 4, 12, 56] (1,5) //[8, 3, 4, 12]
10. splice()
A parameter that deletes start from the start subscript until the end. Returns the deleted number, this directly modifys the original array.
There are two parameters: delete the array element from the start subscript to the end subscript, and return the deleted number. This directly modifys the original array.
There are three parameters, and the elements from the start subscript to the end subscript are replaced with the third parameter. If the first two numbers are the same, it is a replacement. This directly modifys the original array:
var arr=[2,8,3,4,12,56]; //Search from the position of subscript 2((2)) // [3, 4, 12, 56] (arr) // [2, 8] var arr=[2,8,3,4,12,56]; //Delete the positions of subscripts 1 to 5((1,5)) //[8, 3, 4, 12, 56] (arr) // [2] var arr=[2,8,3,4,12,56]; //Replace the position of subscript 1((1,1,'qqq')) //[8] (arr) //[2, “qqq”, 3, 4, 12, 56] var arr=[2,8,3,4,12,56]; //Delete the subscripts 1 to 3 and insert qqq((1,3,'qqq')) //[8, 3, 4] (arr) // [2, “qqq”, 12, 56]
11. concat() can combine two arrays into a new array and return:
var arr1=[1,2,3,4,5]; var arr2=[6,7]; var arr3=(arr2); alert(arr1);// [1,2,3,4,5] alert(arr2);// [6,7] alert(arr3);// [1,2,3,4,5,6,7]
12. Array deduplication:
Method 1:
function removeRepeat(arr){ return (function(elem, pos) { return (elem) == pos; }); }
Method 2:
function removeRepeat(a){ var arr=[]; for(var i=0;i<;i++){ if((a[i]) === -1){ (a[i]); } } return arr; }
Method 3:
function removeRepeat(a){ var arr = []; (function(i){ if((i) === -1){ (i); } }); return arr }
Method 4:
function removeRepeat(arrs){ var newArr = []; var hash = {}; for(var i=0;i<;i++){ var key = typeof(arrs[i])+arrs[i]; if(hash[key] !==1){ (arrs[i]); hash[key] =1; }; }; return newArr; }
12. Prototype attribute, directly change or add functions to the prototype of the array:
//For example, we add a sum method to the array= function(){ var n = 0; (function(i){ n+=i; }); return n; } var arr = [1,2,3,4] alert(()) //10
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.