Apis (methods) in arrays
In learning, path of JavaScript array is a very important part of an array of API is very much, also easy to confuse, every time an array operation to check the document, to learn can’t keep moving on the road, sometimes want to stop to do the summary, I summarized some arrays that are widely used in the API at ordinary times, the hope can help you.
arry.push()
Incrementing an element to the end of an array returns the length of the new array, ary.length
let arry = [2.9.5]
let returnValue = arry.push(4)
console.log(returnValue) / / 4
console.log(arry) // [2, 9, 5, 4]
Copy the code
arry.pop()
To delete the last element from an array, return the deleted element.
let arry = [2.9.5]
let returnValue = arry.pop()
console.log(returnValue) / / 5
console.log(arry) / / [2, 9]
Copy the code
arry.unshift()
Similar to the push method, except that it adds an element to the front of the array and returns the length of the new array, ary.length, as shown in the example
let arry = [2.9.5]
let returnValue = arry.unshift(4)
console.log(returnValue) / / 4
console.log(arry) // [4, 2, 9, 5]
Copy the code
Push and shift can be used to make data ‘first in, first out’, as well as unshift and POP
arry.reverse()
Sort the array in reverse order. Note that it changes the array, not creates a new array, as shown in this example:
let arry = [2.9.5]
arry.reverse()
console.log(arry) // [5, 9, 2]
Copy the code
arry.sort()
To sort an array, accept arguments, which must be functions, or, if not, by character encoding order, as shown in this example:
let arry = [10.5.40.1000]
console.log(arry.sort()) // [10, 1000, 40, 5]
Copy the code
If the numbers want to be sorted by size, write the argument:
let arr = [3.1.7]
console.log(arr.sort((a, b) = > a - b)) // [1, 3, 7]
Copy the code
arry.forEach(item, index)
witharry.map(item, index)
The difference is that the map method returns a new array. For example:
let arry = [1.5.10.15];
let arry1 = arry.map( x= > x + 2);
console.log(arry1) // [3, 7, 12, 17]
Copy the code
arry.some()
If one element satisfies the condition, the expression returns true, and the remaining elements are not tested again. If there are no elements that satisfy the condition, false is returned.
let arry = [1, 5, 10, 15];
console.log(arry.some(item => item > 10)) // true
Copy the code
arry.every()
This expression is used to test whether all elements in an array satisfy the specified condition. This expression returns true only if all elements in an array satisfy the condition, and false otherwise.
let arry = [5.10.15];
console.log(arry.every(item= > item > 2)) // true
Copy the code
arry.filter()
It creates a new array, the original array unchanged, the elements of the new array by checking all the elements in the specified array that meet the conditions, example
let arry = [1.5.10.15];
let arry1 = arry.filter(item= > item > 5)
console.log(arry) // [1, 5, 10, 15]
console.log(arry1) // [10, 15]
Copy the code
arry.join()
Merges the elements of an array into a single string, separated by commas by default if no arguments are taken
let arry = [5.10.15];
console.log(arry.join()) / / 5,10,15
// Add parameters
let arry = [5.10.15];
console.log(arry.join('. ')) / / 5.10.15
Copy the code
arry.splice(index, hm, add)
Index is the starting position, hm is the number of elements to be removed, and add is the number of elements to be added.
let myFish = ['angel'.'clown'.'mandarin'.'sturgeon']
myFish.splice(2.0.'drum') If hm is 0, no elements will be deleted
console.log(myFish) // [ 'angel', 'clown', 'drum', 'mandarin', 'sturgeon' ]
Copy the code
let myFish = ['angel'.'clown'.'mandarin'.'sturgeon']
myFish.splice(2.1.'drum')
console.log(myFish) // [ 'angel', 'clown', 'drum', 'sturgeon' ]
Copy the code
arry.concat()
Use to concatenate two or more arrays and return the concatenated array, as shown in the following example:
let arry1 = [1, 2, 3]
let arry2 = [4, 5, 6]
arry1.concat(arry2)
console.log(arry1.concat(arry2)) // [ 1, 2, 3, 4, 5, 6 ]
Copy the code
These are the common apis for arrays, so you can add them to your collection in case you need them