Common methods of arrays in JavaScript
1,push()
add new content to the end of the array
Parameters: The item to add. Pass multiple separated by commas, any data type is fine
Return value: the length of the added array
Whether to change the original array: change
let ary1 = [12,34,26]; ary1.push(100); //returns a new length length=4console.log(ary1)//The result is [12,34,26,100]
2,pop()
delete the last item of an array
Parameters: none
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]
3,shift()
delete the first item of the array
Parameters: none
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]
4,unshift()
Add new content to the first of the array
Parameter: item to be added, multiple items are 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"]
5,slice()
Find some of them by condition
parameter:
array.slice(n, m), starting from index n to m (excluding m)
array.slice(n) If the second parameter is omitted, it will search until the end
array.slice(0) outputs the content as it is, which can realize array cloning
array.slice(-n,-m) slice supports negative parameters, starting from the last item, -1 is the last item, -2 is the penultimate item
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]; //console.log(ary5.slice(2,8));//Start from index 2 to find the content with index 8, the result is [3, 4, 5, 6, 7, 8] //console.log(ary5.slice(0)); console.log(ary5.slice(-2,-1));//[8]
6,splice()
Add, delete and modify the array
Addition: ary.splice(n,0,m) deletes 0 items from index n, and inserts m or more contents before index n
return empty array
Modification: ary.splice(n,x,m) deletes x from index n, and m replaces the deleted part
Delete the original content and replace it with the new content
Delete: ary.splice(n,m) delete m contents from index n
(If the second parameter is omitted, it is removed from n to the end)
Returns the deleted new array, the original array changed
//Increase 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] //Revise 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]; //console.log(ary6.splice(3,2))//[66, 77] console.log(ary6_s.splice(3));//[66, 77, 88]
7,join()
Concatenates each item of the array into a string with the specified delimiter
Parameter: the specified delimiter (if the parameter is omitted, a comma is used as the 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
8,concat()
for concatenating two or more arrays
Parameters: Parameters can be concrete values or array objects. can be any number of
Return value: Returns the new array after concatenation
Whether to change the original array: no change
let ary8 = ['you']; let ary80 = ary8.concat('it is good'); console.log(ary80);//["Hello"]
9,indexOf()
Detects the position index of the first occurrence of the current value in an array
Parameters: array.indexOf(item,start) item: the element to be searched start: the position in the string to start the search
Return value: the index found for the first time, if not found, return -1
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
10,lastIndexOf()
Detects the position index of the last occurrence of the current value in an array
Parameters: array.lastIndexOf(item,start) item: the element to be searched start: the position in the string to start the search
Return value: the index found for the first time, if not found, return -1
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
11,includes()
Checks if an array contains a specified value
Parameters: the specified 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
12,sort()
Sort the elements of the array (the default is to sort from small to large and according to the string)
Arguments: optional (function) Specifies the collation. The default sort order is ascending alphabetical order
Return value: new array after sorting
Whether to change the original array: change
sort can only process numbers within 10 (single digits) without passing parameters
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; // result [90, 54, 44, 32, 23, 12, 9] }) console.log(ary11);
13,reverse()
reverse the array
Parameters: none
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]
14,forEach()
Loop through each item in the array
Parameters: function ary.forEach(function(item,index,ary){}) item: each item index: index ary: current array
Return value: none
Whether to change the original array: no change
You cannot use continue and break in forEach, you cannot jump out in forEach, you can only skip (return skip)
let ary14 = ['a','b','c','d']; let item = ary14.forEach(function(item,index,ary){ console.log(item,index,ary); })