Array.prototype.concat()
- Used to merge two or more arrays. This method does not change an existing array, but returns a new array
- If the array element is a reference, the copy is of the elementShallow copyIf this data is changed, the change is visible to both the old and new arrays
const arr1 = [1.2] const arr2 = [4.5] const arr3 = [6.7] const res1 = arr1.concat(arr2)/ /,2,4,5 [1] const res2 = arr1.concat(arr2,arr3,10)/ /,2,4,5,6,7,10 [1] Copy the code
Array.prototype.every()
- Tests whether all elements in an array pass the test of a specified function. It returns a Boolean value. If the array is empty, they all return
true
.every()
It doesn’t change the original arrayconst arr1 = [1.2.3.4.5] const res = arr1.every(val= > val > 3) // false Copy the code
Array.prototype.filter()
- Returns a new arrayContains all the elements of the tests implemented by the provided function
const words = [ 'elite'.'exuberant'.'destruction'.'present' ] const res = words.filter(word= > word.length < 6) // [ 'elite' ] Copy the code
Array.prototype.from()
- Creates a new, shallow-copy array instance from an array-like or iterable
Array.from('foo') // [ "f", "o", "o" ] const set = new Set(['foo'.'bar'.'baz'.'foo']) Array.from(set) // [ "foo", "bar", "baz" ] const mapper = new Map([['1'.'a'], ['2'.'b']]) Array.from(mapper.values()) // ['a', 'b'] Copy the code
Array.prototype.sort()
- Sort the elements of an array using the in place algorithm, change and return the array. The default sort order is built when converting elements to strings and then comparing their UTF-16 code unit value sequences
- Parameter: compareFunction(a, b)
- If compareFunction(a, b) is less than 0, then A is placed before B
- If compareFunction(a, b) is greater than 0, b will be placed before A
const array1 = [1.30.4.21.100000] array1.sort() // [1, 100000, 21, 30, 4] array1.sort((a, b) = > a - b) // [1, 4, 21, 30, 100000] array1.sort((a, b) = > b - a) // [100000, 30, 21, 4, 1] Copy the code
Array.prototype.findIndex()
- Returns an array that satisfies the provided test functionThe index of the first element. Returns -1 if no corresponding element is found
const arr = [5.12.8.130.44] const res = arr.findIndex(num= > num > 13) / / 3 Copy the code
Array.prototype.find()
- Returns an array that satisfies the provided test functionValue of the first element. Returns if no corresponding element is found
undefined
const arr = [5.12.8.130.44] const res = arr.findIndex(num= > num > 13) / / 130 Copy the code
Array.prototype.forEach()
- Executes the given function once on each element of the array, returning
undefined
, deleted or uninitialized items will be skipped (for example on a sparse array)const arraySparse = [1.3.7] let numCallbackRuns = 0 arraySparse.forEach(function(element){ console.log(element) / / 1 3 July numCallbackRuns++ / / 3 }) Copy the code
Array.prototype.includes()
- Checks whether an array contains a specified value and returns if it does, depending on the case
true
Otherwise returnfalse
const arr = [5.12.8.130.44] const res = arr.includes(5) // true Copy the code
Array.prototype.indexOf()
- Returns the first index in the array where a given element can be found, or -1 if none exists
const arr = [5.12.8.130.44] const res = arr.indexOf(8) / / 2 const res1 = arr.indexOf(2) // -1 Copy the code
Array.prototype.map()
- Create a new arrayThe result is that each element in the array is the value returned by calling the supplied function once
const array1 = [1.4.9.16] const map1 = array1.map(x= > x * 2) // [2, 8, 18, 32] Copy the code
Array.prototype.pop()
- Removes the last element from the array and returns the value of that element.This method changes the length of the original array
const plants = [1.4.9.16] const res= plants.pop() / / 16 Copy the code
Array.prototype.push()
- Method adds one or more elements to the end of an array and returns the new length of the array
const plants = [1.4.9.16] const res= plants.push(4) / / 5 Copy the code
Array.prototype.reverse()
- Invert the positions of the elements in the array,And returns the array. The first element of the array becomes the last, and the last element of the array becomes the first. This method willChange the original array.
const plants = [1.4.9] const res= plants.reverse() / / [9, 4, 1) Copy the code
Array.prototype.shift()
- Removes the first element from the array and returnsThe value of the element. Returns if the array is empty
undefined
This method,Change the length of the original arrayconst plants = [1.4.9] const res= plants.shift() / / 1 Copy the code
Array.prototype.unshift()
- Adds one or more elements to the beginning of an array and returns thatThe new length of the array, which modifies the original array
const plants = [1.4.9] const res= plants.unshift(3) / / 4 Copy the code
Array.prototype.slice(begin, end)
- Returns a new array/array object, which is a shallow copy of the original array determined by begin and end (begin, but not end). The original array will not be changed
- If end is omitted, slice extracts all the way to the end of the original array
- If the argument is negative, it indicates the penultimate element in the original array at the end of the extraction.
slice(-2,-1)
The penultimate element of the array is extracted to the last elementconst plants = [1.4.9] const res= plants.slice(1) / / [4, 9] Copy the code
Array.prototype.splice(start, deleteCount, item)
- Modify an array by deleting or replacing existing elements or adding new ones in place, and return the modified contents as an array. This method changes the original array.
- parameter
- Start: Specifies the start position of the modification (counting from 0). If the length of the array is exceeded, the contents are appended from the end of the array. If it is negative, it represents the number of bits from the end of the array (counting from -1, which means -n is the NTH element to the last and equivalent to array.length-n); If the absolute value of a negative number is greater than the length of the array, the starting position is bit 0.
- DeleteCount: Integer representing the number of array elements to remove. If it’s 0 or negative then at least one element should be added
- item1, item2… : The element to add to the array, starting at the start position. If not specified, splice() will delete only array elements.
const plants = [1.4.9] const res1 = plants.splice(1.0.1) // [1, 1, 4, 9 const res2 = plants.splice(1.1) // [1, 4, 9] delete const res3 = plants.splice(1.1.3) // [1, 3, 9] Modify/replace Copy the code
Array.prototype.some()
- Tests that at least one element in the array passes the provided function test. It returns a Boolean value.
- If you test with an empty array, it returns false in all cases.
const plants = [1.4.9] const res= plants.some(1) // true Copy the code
Array.prototype.reduce()
- Executes one provided by you for each element in the array
reducer
Function (executed in ascending order) that summarizes its results into a single return value - Accumulator and currentValue are two different values when the callback function is first executed: If initialValue is provided when reduce() is called, accumulator is initialValue and currentValue is the first value in the array. If no initialValue is provided, accumulator takes the first value in the array and currentValue takes the second value in the array.
- Parameters:
- The callback function:
- An accumulator cumulative device
- The current value currentValue
- CurrentIndex indicates the currentIndex
- Array an array
- initialValue:
[0.1.2.3.4].reduce((accumulator, currentValue, currentIndex, array) = > { return accumulator + currentValue }, initialValue) // If initialValue is not provided; The results of the first to fifth operations are 1, 3, 6, and 10 // initialValue = 2; The results of the first to fifth operations are 2, 5, 8, and 12 respectively Copy the code
- The callback function:
- A practical method
- Count the number of occurrences of each element in the array
var names = ['Alice'.'Bob'.'Tiff'.'Bruce'.'Alice']; var countedNames = names.reduce(function (allNames, name) { if (name in allNames) { allNames[name]++; } else { allNames[name] = 1; } return allNames; }, {}); // countedNames: { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } Copy the code
The above excerpt is from MDN