Method of Array object
Array has 21 methods and the difference between pseudo array and array.
method | Return value | Whether to modify the original array |
---|---|---|
push | Modified length | yes |
pop | Value of deleted element | yes |
unshift | Modified length | yes |
shift | Value of deleted element | yes |
revers | Inverted array | yes |
slice | Intercept the value of the element | no |
splice | Value of deleted element | yes |
sort | Sorted array | yes |
toString | Converted String | no |
join | String after adding separator | no |
concat | Spliced array | no |
find | First qualified value | no |
findIndex | First qualified subscript | no |
indexOf | First qualified subscript | no |
includes | Boolean value | no |
every | Boolean value | no |
some | Boolean value | no |
filter | A new array of values of eligible elements | no |
reduce | Value of accumulator | no |
forEach | No return value undefined | no |
map | New array after all elements call the function | no |
push
The push method can accept any number of parameters, add them to the end of the array one by one, and return the modified array length
let arr = [1,2,3] console.log(arr.push(4,5)) // 5 console.log(arr) //[1,2,3,4,5]
This method will change the original array
pop
The pop method removes the last item from the end of the array and returns the value of the removed element
let arr = [1,2,3] console.log(arr.pop()) //3 console.log(arr) //[1,2]
This method changes the original array
unshift
The unshift method adds one or more items from the beginning of the array and returns the modified array length
let arr = [1,2,3] console.log(arr.unshift(-1,0)) //5 console.log(arr) //[-1,0,1,2,3]
This method will change the original array
shift
The shift method removes the first item of the array and returns the value of the moved element
let arr = [2,3,4] console.log(arr.shift()) //2 console.log(arr) //[3,4]
This method will change the original array
revers
The reverse method will reverse the order of the elements in the original array and return the inverted array
let arr = ["a",1,"b",2] console.log(arr.reverse()) //[2, "b", 1, "a"] console.log(arr) //[2, "b", 1, "a"]
This method will change the original array
slice
slice method can intercept the selected element from the array and return the value of the intercepted element
slice(start,end) has two parameters, start position and end position (excluding the end position). If only start is filled in, all elements after the start position will be intercepted
If the parameter value is negative, the position is calculated from the end of the array
let arr = ["a",1,"b",2,"c",3] console.log(arr.slice(1,3)) //[1, "b"] console.log(arr.slice(2)) //["b", 2, "c", 3] console.log(arr.slice(-2)) //["c", 3] console.log(arr.slice(-4,-2)) //["b", 2] console.log(arr) //["a", 1, "b", 2, "c", 3]
This method does not change the original array
splice
The splice method adds / removes items to / from the array and returns the deleted items.
Splice (index position, number of consecutive replacements, new sub item 1, new sub item 2, new sub item 3)
// replace let arr = [1,2,3,4,5,6] console.log(arr.splice(1,2,"two","three")) //[2, 3] console.log(arr) //[1, "two", "three", 4, 5, 6] // delete let arr2 = [1,2,3,4,5,6] console.log(arr2.splice(2,2)) //[3, 4] console.log(arr2) //[1, 2, 5, 6]
This method will change the original array
sort
sort method sorts the array (ASCII) and returns the sorted array
let arr = [10, 1, 32, 3, 5, 6] console.log(arr.sort()) //Sort by ASCII code by default [1, 10, 3, 32, 5, 6] console.log(arr) //Change the original array [1, 10, 3, 32, 5, 6] console.log(arr.sort(sorNum = (a, z) => { return a - z })) // Ascending sort [1, 3, 5, 6, 10, 32] console.log(arr.sort(sorNum = (a, z) => { return z - a })) // Ascending / descending sequence [32, 10, 6, 5, 3, 1]
This method will change the original array
toString
The toString method converts all items of the array into strings separated by commas, and returns the converted string.
let arr = [1,[1,2],["AB",["C"]]] console.log(arr.toString()) //1,1,2,AB,C console.log(arr) //[1,[1,2],["AB",["C"]]]
This method does not change the original array
join
The join method converts the array into a string, and separates the elements with the specified separator. If the separator is not filled in, it is separated by commas by default
let arr = ["a",1,"b",2,"c",3] console.log(arr.join()) //a,1,b,2,c,3 console.log(arr.join("&")) //a&1&b&2&c&3 console.log(arr) //["a", 1, "b", 2, "c", 3]
This method does not change the original array
concat
concat method can connect two or more arrays and return the
let arr = ["a", 1, "b", 2, "c", 3] let arr2 = ["d", 4] console.log(arr.concat(arr2)) // ["a", 1, "b", 2, "c", 3, "d", 4] console.log(arr) //["a", 1, "b", 2, "c", 3] console.log(arr2.concat("e", 5)) //["d", 4, "e", 5]
This method does not change the original array
find
The find method can return the first qualified value in the array. Short circuit principle. If there is no qualified value, it will return undefined
let arr = ["a", 1, "b", 1, "c", 3] console.log(arr.find(one = (item) => { return item == 1 })) //1 console.log(arr.find(one = (item) => { return item > 2 })) //3 console.log(arr) //["a", 1, "b", 1, "c", 3]
This method does not change the original array
findIndex
findIndex method can return the index of the first qualified value in the array. Short circuit principle. If there is no qualified value, it will return -1
let arr = ["a", 2, "b", 1, "c", 3] console.log(arr.findIndex(one = (item) => { return item > 1 })) //1 console.log(arr.findIndex(three = (item) => { return item == 3 })) //5 console.log(arr) //["a", 2, "b", 1, "c", 3]
This method does not change the original array
indexOf
indexOf method searches for the specified item in the array and returns its position. Short circuit principle. If there is no qualified value, it returns -1
let arr = ["a", 2, "b", 2, "c", 3] console.log(arr.indexOf("b")) //2 console.log(arr.indexOf(2)) //1 console.log(arr) //["a", 2, "b", 2, "c", 3]
This method does not change the original array
lastIndexOf is to search from the end
includes
The includes method looks up whether the array contains the specified value, returns true if included, and returns false if not included
let arr = ["a", 1, "b", 2, "c", 3] console.log(arr.includes(5)) //false console.log(arr.includes(2)) //true console.log(arr) //["a", 1, "b", 2, "c", 3]
This method does not change the original array
every
every method checks whether all values in the array pass the test. The short-circuit principle returns true if all values pass. As long as one item in the array fails, it returns false
let arr = [1, 2, 3, 4] console.log(arr.every(five = (x) => { return x > 5 })) //false console.log(arr.every(zero = (x) => { return x > 0 })) //true console.log(arr) //[1, 2, 3, 4]
This method does not change the original array
some
The every method checks whether there is a value in the array that passes the test. The short-circuit principle returns false if it fails. As long as there is one item in the array that passes, it returns true
let arr = [1, 2, 3, 4] console.log(arr.some(five = (x) => { return x > 5 })) //false console.log(arr.some(five = (x) => { return x > 3 })) //true console.log(arr) //[1, 2, 3, 4]
This method does not change the original array
filter
The filter method creates a new array, fills in the tested values in the array, and returns a new array. If there is no tested value, it returns an empty array
let arr = [1, 2, 3, 4] console.log(arr.filter(even = (x) => { return x % 2 == 0 })) //Returns all even arrays [2,4] console.log(arr.filter(ex1 = (x) => { return x - 1 > 0 })) //Returns all arrays greater than 1 [2,3,4] console.log(arr) //[1, 2, 3, 4]
This method does not change the original array
reduce
The reduce method receives a function as an accumulator, and reduce executes a callback function for each element in the array in turn to return the value of the accumulator
let arr = [1, 2, 3, 4] console.log(arr.reduce(sum = (acc, cur) => { return acc + cur })) //Accumulate the value of the entire array 10 console.log(arr.reduce(sum = (acc, cur) => { return acc + cur }, 1)) //Add up the values of the entire array plus 11 console.log(arr.reduce(sum = (acc, cur) => { return acc - cur })) //subtract consecutively console.log(arr) //[1, 2, 3, 4]
This method does not change the original array
forEach
The forEach method calls the function once for each element in the array in order, and the function returns undefined
var arr2 = [12, 5, 8, 1, 4]; arr2.forEach(function(item) { console.log(item) }) //Print each value in the array arr2.forEach(function(item, index) { if (item > 1) { console.log(index) } }) //If the value of an element in arr2 is greater than 1, the subscript of this element is printed
This method does not change the original array
map
The map method uses the result of calling the function for each array element to create a new array, and returns the new array after calling the function
let arr = [1, 2, 3, 4] console.log(arr.map(one = (x) => { return x + 2 })) //The value of each element in the array +2 [3, 4, 5, 6] console.log(arr.map(one = (x) => { return x ** 3 })) //The third power of the value of each element in the array [1, 8, 27, 64] console.log(arr) //[1, 2, 3, 4]
This method does not change the original array
Pseudo array
arguments is an array like object that passes function parameters, which is similar to an array, but there are no array attributes except the length attribute and index elements, so array methods cannot be used.
The method of using an array needs to convert arguments into an array
function es5(arr) { console.log(Array.from(arguments)) } es5([1,2,3,4]) // es5 method transform array function es6(arr) { console.log(...arguments) } es6([1,2,3,4]) // es6 method transform array
The arguments object is a local variable available in all (non arrow) functions.