1.length() method
Gets the length of the array
let a = [1,2,3,4,5,6]; console.log(a.length); // 6 starts with 1
2.join() method, toString() method and toLocaleString() method
Array to string
let a = [1, 2, 3, 4, 5, 6]; console.log(a.join()); //1,2,3,4,5,6 console.log(a.join("?")); //1?2?3?4?5?6
3.reverse() method
Invert the order of array elements
let a = [1, 2, 3, 4, 5, 6]; console.log(a.reverse()); // [6, 5, 4, 3, 2, 1]
4.slice() method
Intercept the array (the original array will not be affected)
let a = [1, 2, 3, 4, 5, 6]; console.log(a.slice(2)); //[3, 4, 5, 6] console.log(a.slice(-2)); //[5, 6] when the parameter is negative, it is from back to front console.log(a.slice(2, 4)); //[3, 4] console.log(a.slice(2, -2)); //[3, 4] the two parameters are from the first to the second
5.concat() method
Spliced array (does not affect the original array)
let a = [1, 2, 3, 4, 5, 6]; console.log(a.concat([7, 8, 9])); //[1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(a.concat(7, 8, 9)); //[1, 2, 3, 4, 5, 6, 7, 8, 9] console.log(a.concat("7,8,9")); //[1, 2, 3, 4, 5, 6, '7, 8, 9'] add the same as other types
6.splice() method
Commonly used to delete array elements
let a = [1, 2, 3, 4, 5, 6]; console.log(a.splice(2)); //[3, 4, 5, 6] a parameter represents all from the first few, and returns an array of deleted elements console.log(a.splice(1, 2)); //[3, 4]
7.push() method
Adding one or more elements to the end of an array returns the length of the array
let a = [1, 2, 3, 4, 5, 6]; console.log(a.push(7)); //7 console.log(a.push(7, 8)); //8 console.log(a.push([7, 8])); //7 the array here should be [1,2,3,4,5,6, [7,8]]
8.pop() method
Deletes the last element of the array and returns the last element (only one is deleted)
let a = [1, 2, 3, 4, 5, 6]; console.log(a.pop()); //6
9.shift() method
Deletes the first element of the array and returns the value of the first element
let a = [1, 2, 3, 4, 5, 6]; console.log(a.shift(0)); //1
10.unshift() method
Adds one or more elements to the beginning of the array and returns a new length
let a = [1, 2, 3, 4, 5, 6]; console.log(a.unshift(0)); //7 console.log(a.unshift(7, 8)); //8 console.log(a.unshift([7, 8])); //7
11.indexOf() method and lastIndexOf() method
Query the array, return - 1 if there is no result, and return the subscript of the result if there is a result
let a = [1, 2, 3, 4, 5, 6]; console.log(a.indexOf(2)); //1 console.log(a.indexOf(2, 2)); //-1. The second parameter starts from the first one
12.sort() method
By default, if the sort() method does not pass the comparison function, it is in alphabetical ascending order by default. If the element is not a string, the toString() method will be called to convert the element into the Unicode (universal code) site of the string, and then compare the characters. So sorting data by default is problematic.
let a = [20,10,2,1,3]; a.sort();// [1, 10, 2, 20, 3] a.sort(function(a,b){ return a-b; //Ascending order }); //[1, 2, 3, 10, 20] a.sort(function(a,b){ return b-a; //Descending order }); //[20,10,3,2,1]
13.forEach() method
Traversal array
let a = [1, 2, 3, 4, 5, 6]; a.forEach((e)=>{ console.log(e); })
14.map() method
Use each element of the array and pass it to the specified function without changing the original array
let a = [1, 2, 3, 4, 5, 6]; let b = a.map((e) => { return e * e; }); console.log(b) //[1, 4, 9, 16, 25, 36];
15.filter() method
Filtering method
let a = [1,2,3,4,5,6,3,1]; a.filter(function(v,i,self){ return self.indexOf(v) == i; }); //[1, 2, 3, 4, 5, 6]
16.every() method and some() method
every() determines whether each item in the array meets the condition. Only when all items meet the condition will it return true.
some() determines whether there are items that meet the conditions in the array. As long as one item meets the conditions, it will return true.
var a = [1, 2, 3, 4, 5, 6]; a.every(x=>x>0);//return true; a.every(x=>x>5);//return false; a.some(x=>x>5);//return true;
17.reduce() method and reduceRight() method
reduce() has two parameters: the function and the initial value of recursion. Start with the first item of the array and go through it one by one to the end
Reducereight() starts from the last item of the array and traverses forward to the first item
var a=[1,2,3,4]; a.reduce(function(a,b){ return a+b; }); //10