Summary of array methods
Push () method
Adds one or more new elements to the end of the array and returns the end increment of the length of the new array
let arr = [1.2.3.4]
console.log(arr.push(5)) //5 returns the length of the new array
console.log(arr) / / [1, 2, 3, 4, 5]
Copy the code
Pop () method
Deletes the last element of the array and returns the last element of the array to be deleted
let arr = [1.2.3.4]
let num = arr.pop()
console.log(num) //4, return the deleted element
console.log(arr) / / [1, 2, 3]
Copy the code
Unshift () method
Adds one or more elements to the front of the array and returns a new length header addition
let arr = [1.2.3.4]
console.log(arr.unshift(5.1)) // return the length of the new array
console.log(arr) / /,1,1,2,3,4 [5]
Copy the code
The shift () method
Deletes the array head element and returns the deleted element head deletion
let arr = [1.2.3.4]
let num = arr.shift()
console.log(num) //1 returns the length of the new array
console.log(arr) / / / 2 and 4
Copy the code
Splice () method
Deletes and modifies an array to return an array of deleted elements
Syntax: splice (index, length, added element 1, added element 2…)
Parameter description:
Index: Index of the element to be deleted
Length: indicates the length to be deleted
Added element: The element added starting with index is added to the deleted array to add the new array
let arr = [1.2.3.4.5]
// select the element with index 1 from the array and delete 3 elements ====> delete 2,3,4
// Add elements 1,[1,2],[1,2,3] from array index 1
let reArr3 = arr.splice(1.3.1[1.2], [1.2.3])
console.log(reArr3) //[2,3,4], returns an array of deleted elements
console.log(arr) / / [1, 1, [1, 2], [1, 2, 3], 5]
Copy the code
Slice () method
Clipping the current array returns a new array containing the clipped values, unchanged from the original array
Syntax: slice (starIndex, endIndex)
Parameter description:
StarIndex: The starting position of the clipping, which returns an array containing the value of the starting position
EndIndex: position at the end of the clipping, excluding the value of the end position
let arr = [1.2.3.4.5]
let newArr = arr.slice(2.4)
console.log(newArr) / / [3, 4]
Copy the code
Concat () method
Merges two or more arrays, returns a new array, does not alter the original array
let a = [1.2.3]
let b = [2.3.4]
let c = a.concat(b)
console.log(c) / /,2,3,2,3,4 [1]
Copy the code
The join () method
Converts an array to a string without changing the original array. This method returns the converted string, separated by, by default
let arr = [1.2.3.4]
console.log(arr.join())/ / 1, 2, 3, 4
console.log(arr)/ / [1, 2, 3, 4]
Copy the code
The reverse () method
Used to reverse the order of the elements in an array, returning the reversed array, changing the original array
let arr = [1.2.3.4.5]
let re = arr.reverse()
console.log(re) / /,4,3,2,1 [5]
console.log(arr)/ /,4,3,2,1 [5]
Copy the code
IndexOf() and lastIndexOf() methods
Both return the element’s first position in the array, or -1 if not found
The difference is: indexOf looks up from the beginning. LastIndexOf looks up from the tail
Syntax: indexOf(element, startIndex) lastIndex(element, startIndex)
Parameter Description:
Element: The element to be found
StartIndex: the index to start the search
The filter () method
Returns the elements that meet the criteria to form a new array, unchanged
Grammar: filter (function (item, index, ‘arry) {})
Parameter Description:
- The callback determines whether item meets the criteria on each iteration
let arr = [1.2.3.4.5]
let re = arr.filter((item) = >{
return item > 3
})
console.log(arr) / / [1, 2, 3, 4, 5]
console.log(re) / / (4, 5)
Copy the code
The map () method
Each element in the array is called once, and the callback is returned in a new value, unchanged
Map (function(item,index,arr){}
Parameter Description:
- Is a callback function that is executed on the mig element during traversal
let arr = [1.2.3.4]
let re = arr.map((item,index,arr) = >{
return item*10
})
console.log(re);// [10, 20, 30, 40];
console.log(arr);// [1, 2, 3, 4]
Copy the code
Every () method
Determines whether an element in an array satisfies a condition. Returns true if each element satisfies a condition, false otherwise
Every (function(item,index,arr){})
let array = [1.2.3.4];
let bo = array.every((item, index, array) = >{return item > 2;
});
console.log(bo); // false;
Copy the code
Some () method
Search for a value that satisfies the condition, and stop immediately if it is found, returning true if it is found, false if it is not.
Grammar: some (function (item, index, arr) {})
let array = [1.2.3.4.6];
let temp = array.some((item, index, array) = > {
return item > 5;
});
console.log(temp);// true
Copy the code
The forEach () method
Iterate through the array without interrupting
Grammar: forEach (function (item, index, array) {}, this)
Parameter Description:
Item: The element currently traversed by the array. By default, the array elements are fetched from left to right
Index: The index of the current elements in the array. The first element has an index of 0, which is accumulated successively
Arr: array currently traversed
This: The reference to this in the callback function
let arr = [1.2.3.4.5]
let obj = {a:1}
arr.forEach(function(item,index,arr){
console.log(The current element is${item}The index for${index}, belongs to an array${arr}`);
console.log(this);/ / this point to obj
},obj)
Copy the code