1, the push ()
Adds new content to the end of the array
Parameter: item to add. Multiple passes are separated by commas and can be of any data type
Return value: The length of the new array
Whether to change the original array: change
let ary1 = [12.34.26];
ary1.push(100); // Return a new length
length=4console.log(ary1)// result is [12,34,26,100]
Copy the code
2, pop ()
Deletes the last item of the array
Parameters: no
Return value: deleted item
Whether to change the original array: change
let ary2 = [108.112.39.10];
ary2.pop();// The last item removed is 10
console.log(ary2);/ / [108, 112, 39]
Copy the code
3, the shift ()
Deletes the first item of the array
Parameters: no
Return value: deleted item
Whether to change the original array: change
let ary3 = [0.108.112.39];
ary3.shift();// The first item removed is 0
console.log(ary3);/ / [108, 112, 39]
Copy the code
4, unshift ()
Adds new content to the top of the array
Parameter: Item to be added, separated by ‘,’
Return value: The length of the new array
Whether to change the original array: change
let ary4 = ['c'.'d'];
ary4.unshift('a'.'b');
console.log(ary4);//["a", "b", "c", "d"]
Copy the code
5, slice ()
Find out part of the content according to the conditions
Parameters:
Array.slice (n, m) from index n to m (without m)
Array.slice (n) if the second argument is omitted, the search continues until the end
Array.slice (0) output the original content, can implement array cloning
Array. slice(-n,-m) Slice supports negative arguments. -1 is the last entry and -2 is the penultimate entry from the last entry
Return value: Returns a new array
Whether to change the original array: No change
let ary5 = [1.2.3.4.5.6.7.8.9];
/ / the console. The log (ary5. Slice (2, 8)); [3, 4, 5, 6, 7, 8] [3, 4, 5, 6, 7, 8]
//console.log(ary5.slice(0));
console.log(ary5.slice(-2, -1));/ / [8]
Copy the code
6, splice ()
Add, delete, or modify an array
Add: ary.splice(n,0,m) deletes 0 items from index n, and inserts M or more items before index n
Return an empty array
Change: ary.splice(n,x,m) deletes x numbers from index n, m replaces the deleted parts
Delete the original content and replace it with new content
Delete: ary.splice(n,m) deletes m items from index n
(If the second argument is omitted, it is removed from n to the end)
Returns deleted new array, original array changed
/ / add
let ary6_z = [33.44.55.66.77.88];
ary6_z.splice(2.0.'a'.'b')
console.log(ary6_z);// [33, 44, "a", "b", 55, 66, 77, 88]
/ / modify
let ary6_x = [33.44.55.66.77.88];
ary6_x.splice(1.2.'x'.'y')
console.log(ary6_x);// [33, "x", "y", 66, 77, 88]
/ / delete
let ary6_s = [33.44.55.66.77.88];
/ / the console log (ary6 splice (3, 2)) / / [66, 77]
console.log(ary6_s.splice(3));/ / [66, 77, 88]
Copy the code
7, the join ()
Concatenates each item of the array into a string using the specified delimiter
Argument: Delimiter specified (if omitted, comma is used as delimiter)
Return value: concatenated string
Whether to change the original array: No change
let ary7 = [1.2.3];
console.log(ary7.join(', '));//1, 2, 3
Copy the code
8, concat ()
Join two or more arrays
Arguments: Arguments can be concrete values or array objects. It can be as many as you want
Return value: Returns the new array after joining
Whether to change the original array: No change
let ary8 = ['you'];
let ary80 = ary8.concat('good');
console.log(ary80);//[" you ", "ok "]
Copy the code
9, indexOf ()
Checks the index of the first occurrence of the current value in the array
Parameter: array.indexof (item,start) item: the element to search for start: the position in the string at which the search begins
Return value: the first search index, if no, -1 is returned
Whether to change the original array: No change
let ary9 = ['a'.'b'.'c'.'d'.'e'.'a'.'f'];
console.log(ary9.indexOf('c'));/ / 2
console.log(ary9.indexOf('a'.3))/ / 5
Copy the code
10, lastIndexOf ()
Checks the index of the last occurrence of the current value in the array
Parameter: array.lastIndexof (item,start) item: the element to search for start: the position in the string at which the search begins
Return value: the first search index, if no, -1 is returned
Whether to change the original array: No change
let ary10 = ['a'.'b'.'c'.'d'.'e'.'a'.'f'];
console.log(ary10.lastIndexOf('c'));/ / 2
console.log(ary10.lastIndexOf('f'.1))/ / 1
Copy the code
11, includes ()
Checks whether an array contains a specified value
Parameter: specifies the content
Return value: Boolean
Whether to change the original array: No change
let ary13 = ['a'.'b'.'c'.'d'];
console.log(ary13.includes('c'));//true
console.log(ary13.includes(2));//false
Copy the code
12, the sort ()
Sort the elements of an array (by default, sort from smallest to largest and by string)
Parameter: Optional (function) Specifies the sort rule. The default sort order is alphabetical ascending
Return value: new sorted array
Whether to change the original array: change
Sort can only handle numeric sorts up to 10 (single digits) without passing an argument
let ary11 = [32.44.23.54.90.12.9];
ary11.sort(function(a,b){ // return a-b; // Result [9, 12, 23, 32, 44, 54, 90]
// return b-a; [90, 54, 44, 32, 23, 12, 9]})
console.log(ary11);
Copy the code
13 and reverse ()
Arrange the array upside down
Parameters: no
Return value: new array in reverse order
Whether to change the original array: change
let ary12 = [6.8.10.12];
console.log(ary12.reverse());/ / [12, 10, 8, 6]
Copy the code
14 and the forEach ()
Loop through each item in the set
ForEach (function(item,index,ary){}) item: each item index: the index ary: the current array
Returned value: None
Whether to change the original array: No change
ForEach: “continue” and “break” cannot be used in forEach. ForEach: “skip” cannot be used in forEach.
let ary14 = ['a'.'b'.'c'.'d'];
let item = ary14.forEach(function(item,index,ary){
console.log(item,index,ary);
})
Copy the code
So that’s what the common array methods are.