1. An array is an ordered collection of values. Each value is called an element, and each element has an index in the array. Js arrays are weakly typed and can contain elements of different types. Arrays can even be objects or other arrays.

var arr = [1.true.null.undefined, {x:1},1.2.3]].Copy the code

Array size (0 to 2^23-1)

  • Literal creation
var BAT = ["Alibaba"."Tencent"."Baidu"];
var students = [{name:'Bson'.age:27}, {name:'Nunnly'.age:3}];
var arr = [1.true.null.undefined, {x:1},1.2.3]].var arrInArr = [[1.2], [3.4.5]].var Arr = [,,1.2];
Copy the code
  • new Array
var arr = new Array(a);var arrWithLength = new Array(100); //undefined*100
var arRLikesLiteral = new Array(true.false.null.1.2."hi");// equivalent to [true,false,1,2,"hi"];
Copy the code

3. Read and write array elements

var arr = [1.2.3.4.5];
arr[1]; / / 2
arr.length; / / 5
arr[5] = 6;
arr.length = 6;
delete arr[0];
arr[0]; //undefined
Copy the code

4. Add and delete array elements (array elements are dynamic and do not need to specify size)

var arr = [];
arr[0] = 1;
arr[1] = 2;
arr.push(3);
arr; / / [1, 2, 3]

arr[arr.length] = 4; //equal to arr.push(4)
arr; / / [1, 2, 3, 4]

arr.unshift(0); 
arr; / /,1,2,3,4 [0]

delete arr[2];
arr; / / [0, 1, undefined, 3, 4]
arr.length; / / 5
2 in arr; //false

arr.length -= 1;
arr; / / [0, 1, undefined, 3] 4 is removed

arr.pop(); //3returned by pop
arr; / / [0, 1, undefined] 3 is removed

arr.shift(); // 0 returned by shift
arr; //[1,undefined]
Copy the code

5. Array iteration

For loop for… in loop

6. Sparse arrays

Sparse array: Does not have a continuous index starting at 0. Generally, the length attribute value is larger than the actual number of elements

Array.prototype []=> array.prototype

  • Join array to string (default,)
var arr = [1.2.3];
arr.join(); / / "1, 2, 3"
arr.join("_"); / / "1 _2_3"

function repeatString(str,n){
    return new Array(n+1).join(str);
}
repeatString("a".3); // "aaa"
repeatString("Hi".5); //"HiHiHiHiHi"
Copy the code
  • Reverse changes the array
var arr = [1.2.3];
arr.reverse(); / / [3, 2, 1]
arr; //[3,2,1] the original array is modified
Copy the code
  • Sort (default alphabetical order) modifies the array
var arr = ["a"."d"."c"."b"];
arr.sort(); // ["a","b","c","d"]

arr = [13.24.51.3];
arr.sort(); / /,24,3,51 [13]
arr; / /,24,3,51 [13]

arr.sort(function(a,b){
    return a-b;
}); / /,13,24,51 [3]

arr = [{age:25}, {age:39}, {age:99}];
arr.sort(function(a,b){
    return a.age - b.age
});
arr.forEach(function(item){
    console.log('age',item.age)
});
//result:
//age 25
//age 39
//age 99
Copy the code
  • Concat array merge does not change the original array
var arr = [1.2.3];
arr.concat(4.5); / / [1, 2, 3, 4, 5]
arr; / / [1, 2, 3]

arr.concat([10.11].13); / /,2,3,10,11,13 [1]
arr.concat([1[2.3]]) //[1,2,3,1,[2,3]] is an array
Copy the code
  • Slice clipping returns part of the array left closed right open the original array is not modified
var arr = [1.2.3.4.5];
arr.slice(1.3); / / [2, 3]
arr.slice(1); / / 5-tetrafluorobenzoic [2]
arr.slice(1, -1); //[2,3,4] -1 last element in table
arr.slice(-4, -3); / / [2]
Copy the code
  • Splice Array splice changes the original array
var arr = [1.2.3.4.5]
arr.splice(2); / / returns/three, four, five
arr; / / [1, 2];

arr = [1.2.3.4.5];
arr.splice(2.2); //returns [3,4] the second parameter represents the number of deleted elements
arr; / /,2,5 [1]

arr = [1.2.3.4.5];
arr.splice(1.1.'a'.'b'); //returns [2] The first parameter indicates the number of columns to be removed, the second parameter indicates the number to be removed, and the third parameter indicates the element to be added
arr; / / (1, "a", "b", three, four, five]
Copy the code
  • ForEach array traversal the first argument represents the element value the second argument represents the index and the third argument represents the array itself IE9 and above
var arr = [1.2.3.4.5];
arr.forEach(function(x,index,a){
    console.log(x+'|'+index+'|'+(a===arr));
});
// 1|0|true
// 2|1|true
// 3|2|true
// 4|3|true
// 5|4|true
Copy the code
  • The map array map does not change the conversion of each element in the array
var arr = [1.2.3];
arr.map(function(x){
    return x+10
}); / / [11]
arr; / / [1, 2, 3]
Copy the code
  • Filter Array Does not change the original array
var arr = [1.2.3.4.5.6.7.8.9.10];
arr.filter(function(x,index){
    return index % 3= = =0 || x >= 8;
}); / / return,4,7,8,9,10 [1]
arr; / /,2,3,4,5,6,7,8,9,10 [1]
Copy the code
  • Every some array
var arr = [1.2.3.4.5];
arr.every(function(x){
    return x < 10;
}) //true
arr.every(function(x){
    return x<3;
}); //false
var arr = [1.2.3.4.5];
arr.some(function(x){
    return x === 3;
}) //true
arr.some(function(x){
    return x === 100;
}) //false
Copy the code
  • Reduce reduceRight The original array does not change
var arr = [1.2.3];
var sum = arr.reduce(function(x,y){
    return x+y
},0); //6 The second parameter is optional
arr; / / [1, 2, 3]

arr = [3.9.6];
var max = arr.reduce(function(x,y){
    console.log(x+"|"+y);
    returnx>y? x:y });/ / 3 | 9
/ / | 9 6
max; / / 9

max = arr.reduceRight(function(x,y){
    console.log(x+"|"+y);
    returnx>y? x:y });/ / | 6 to 9
/ / 9 | 3
max; / / 9
Copy the code
  • IndexOf lastIndexOf array retrievals return element positions or -1
var arr = [1.2.3.2.1];
arr.indexOf(2); / / 1
arr.indexOf(99); / / 1
arr.indexOf(1.1); // start with the second element
arr.indexOf(1, -3); / / 4
arr.indexOf(2, -1); / / 1
arr.lastIndeXOf(2); / / 3
arr.lastIndexOf(2, -2); / / 3
arr.lastIndexOf(2, -3); / / 1
Copy the code
  • Array.isarray () checks whether the element is an Array
Array.isArray([]); //true
[] instanceof Array; //true
({}).toString.apply([]) === '[object Array]'; //true
[].constructor === Array; //true
Copy the code

8. Arrays vs. general objects

9. Arrays vs. strings