Pusgh () adds an array

1. The push method returns the length of the added arrayCopy the code
let arr = [1.2.3.4.5]
console.log(arr.push(5))   / / 6
console.log(arr) / /,2,3,4,5,5 [1]
Copy the code

Unshift () adds the array

2. Unshift () adds the element from the front and returns the length of the arrayCopy the code
let arr = [1.2.3.4.5]
console.log(arr.unshift(2))    / / 6
console.log(arr)  / /,1,2,3,4,5 [2]
Copy the code
  • Pusgh () and unshift() both add an array and return the added length. The difference is that pusgh is added to the end of the array and unshift is added to the front of the array

Pop () deletes the array

3. Pop is deleted from the back of the array. Only one element can be deleted, and the return value is the deleted elementCopy the code
 let arr = [1.2.3.4.5]
 console.log(arr.pop())  / / 5
 console.log(arr)  / / [1, 2, 3, 4]
Copy the code

Shift () deletes the array

4. Pop is deleted from the front of the array, and only one element can be deleted. The return value is the deleted elementCopy the code
let arr = [1.2.3.4.5]
console.log(arr.shift())  / / 1
console.log(arr)   / / 5-tetrafluorobenzoic [2]
Copy the code

Spalice (I,n) deletes the specified element

5. Delete n elements starting from I (index value). The return value is the deleted elementCopy the code
let arr = [1.2.3.4.5]
console.log(arr.splice(2.2))     / / [3, 4]
console.log(arr)    / /,2,5 [1]
Copy the code

Find the object whose id is 2 and delete it

const array = [
  { id: 1.name: 'Joe' },
  { id: 2.name: 'four zhang' },
  { id: 3.name: 'fives'},]; array.splice( array.findIndex((item) = > item.id == 2),
  1,);console.log(array); / / / {id: 1, name: 'Joe'}, {id: 3, name: 'fives'},]
Copy the code
Conclusion: 1. 2 p with shift are deleted array and intelligence to delete a pop is to remove the front of the value, is behind the shift remove 2 shplice delete an array, you can delete multiple shplice (I, n) remove elements from the set I started at the back of the n elementsCopy the code

Concat () joins/merges arrays

6. Connect two arrays and return the value of the new arrayCopy the code
let arr = [1.2.3.4.5]
console.log(arr.concat([1.2]))  / /,2,3,4,5,1,2 [1]
console.log(arr)   / / [1, 2, 3, 4, 5]
Copy the code

Split () string to array

7. Convert strings to arraysCopy the code
let str = '123456'
console.log(str.split(' ')) // ["1", "2", "3", "4", "5", "6"]
Copy the code

Sort () sort

8. Sort the array. The return value is a sorted array. By default, sort the array by the leftmost number, not by sizeCopy the code
let arr = [2.10.6.1.4.22.3]
console.log(arr.sort())   // [1, 10, 2, 22, 3, 4, 6]
Copy the code
8.2 We can set the order of the array from smallest to largestCopy the code
let arr1 = arr.sort((a, b) = >a - b)  
console.log(arr1)   // [1, 2, 3, 4, 6, 10, 22]
Copy the code
8.3 We can set the order of the array from large to smallCopy the code
let arr2 = arr.sort((a, b) = >b-a)  
console.log(arr2)  // [22, 10, 6, 4, 3, 2, 1]
Copy the code

The reverse () inversion

9. Array inversion returns the inverted array and changes the original arrayCopy the code
let arr = [1.2.3.4.5]
console.log(arr.reverse())    / /,4,3,2,1 [5]
console.log(arr)    / /,4,3,2,1 [5]
Copy the code

Slice () slits an array

10. Cut the array from start to end. End does not contain the value of endCopy the code
let arr = [1.2.3.4.5]
console.log(arr.slice(1.3))   / / [2, 3]
console.log(arr)    / / [1, 2, 3, 4, 5]
Copy the code

ForEach () traverses the array

Value: current array, index: index, array: original arrayCopy the code
  • ForEach () is an asynchronous function
  • ForEach () is an asynchronous function
  • ForEach () is an asynchronous function
  • Say important things three times
let arr = [1.2.3.4.5]
arr.forEach( (value,index,array) = >{
     console.log(`value:${value}    index:${index}     array:${array}`)})//  value:1    index:0     array:1,2,3,4,5
    //  value:2    index:1     array:1,2,3,4,5
    //  value:3    index:2     array:1,2,3,4,5
    //  value:4    index:3     array:1,2,3,4,5
    / / value: index: 4 array: 1, 2, 3, 4, 5
Copy the code

To verify the asynchrony of foreach, here’s an example

let arr = [1.2.3.4.5]

arr.forEach( (value,index,array) = >{
        value = value * 2
    })   
    console.log(arr)  [1.2.3.4.5]
Copy the code

Here we expect each value of X5 to yield [5,10… The console still prints [1, 2, 3, 4, 5], so it is not appropriate to get the changed array foreach!

Map (callback) mapping traverses a number group

12. Map is also an iterated array and has a return value. The return value is a new array.Copy the code
var arr1 = [1.2.3.4]; 
var res1 = arr1.map((item,index,arr) = >{ 
 item = item * 3; 
 return item; 
})
console.log(arr1); / / [1, 2, 3, 4]
console.log(res1); / /,6,9,12 [3]
Copy the code

So we can see the difference between map and forEach.

Filter (callback) Filter search

13 Filter/search an array and return an array that meets the requirements without changing the original arrayCopy the code
let arr = [1.2.3.4.5]
let arr1 = arr.filter( (value, index) = > value<3)
console.log(arr1)    / / [1, 2]
Copy the code

Every (the callback) : Boolean search

14 Check whether all elements of the array are met. If yes, return trueCopy the code
let arr = [1.2.3.4.5]
let arr1 = arr.every( (value, index) = >value<3)
console.log(arr1) // false
let arr2 = arr.every( (value, index) = >value<6)
console.log(arr2)  // true
Copy the code

Some (the callback) : Boolean search

15 Check whether any of the elements in the array meet the criteria. If any of the elements meet the criteria, return trueCopy the code
let arr = [1.2.3.4.5]
let arr1 = arr.every( (value, index) = >value<3)
console.log(arr1) // false
let arr2 = arr.every( (value, index) = >value<6)
console.log(arr2)  // true
Copy the code

Reduce (callback) accumulation/calculation

16 Iterates through all the items in the array, accumulator, merges each value in the array (from left to right), and finally evaluates to a value callback: PreviousValue Mandatory -- the value returned by the last callback call, CurrentValue mandatory -- the current array item being processed index optional -- the index value of the current array item in the array optional -- the original array initialValue: optional -- the initialValueCopy the code
let arr = [0.1.2.3.4]
let arr1 = arr.reduce((preValue, curValue) = > 
    preValue + curValue
)
console.log(arr1)    / / 10
Copy the code
Don’t forget to like it if you find it useful!