Find: Iterate over the search, returning the first element that meets the criteria

let re = [6, 2, 7, 4, 5, 9].find( function (item, index, o) { console.log( item, index, o ); return item > 6; });Copy the code

Output: 7

FindIndex: Traverses the search and returns the index value of the first element that meets the condition

let re2 = [6, 2, 7, 4, 5, 9].findIndex(function (item, index) {
    return item > 6
 })
console.log(re2)
Copy the code

Output: 2

Some: Traverses the search, returning true if one of the criteria is met, false otherwise

let re3 = [6, 2, 7, 4, 5, 9].some(function (item, index) {
    return item > 6
 })
console.log(re3)
Copy the code

Output: true,

Every: returns true if every element meets the criteria, false otherwise

let re4 = [6, 2, 7, 4, 5, 9].every(function (item, index) {
     return item > 6
 })
 console.log(re4)
Copy the code

Output: false

Filter: To filter out the elements that meet the conditions and return a new array

let re5 = [6, 2, 7, 4, 5, 9].filter(function (item, index) {
     return item > 6
})
 console.log(re5)
Copy the code

Output: array [7, 9]

Map: Iterates through the array, having each element perform a callback function to return all the results into the new array

let re6 = [6, 2, 7, 4, 5, 9].map(function (item, index) {
     return item * item
})
 console.log(re6)
Copy the code

Output: array [36, 4, 49, 16, 25, 81]

Push () method: adds a value to the end of the array

let ary = [1, 2, 3, 4]
 ary.push('abc', 'bcd')
 console.log(ary)
Copy the code

Output: array [1, 2, 3, 4, “ABC “,” BCD “]

Unshift (): Adds a value to the beginning of the array

let ary = [1, 2, 3, 4]
ary.unshift('abc', 'bcd')
console.log(ary)
Copy the code

Output: array [” ABC “, “BCD “, 1, 2, 3, 4]

Pop (): Removes a value from the array, from the end

let ary = [1, 2, 3, 4]
let res = ary.pop()
console.log(ary)
console.log(res)
Copy the code

Output: arrays [1, 2, 3] and 4

Shift (): Removes a value from the array, from the start

let ary = [1, 2, 3, 4]
let res = ary.shift()
console.log(ary)
console.log(res)
Copy the code

Output: arrays [2, 3, 4] and 1

Splice () method

Splice (index, count): deletes a value from a specified position

Index indicates the position in the array to be deleted from, which is an index number

Count indicates how many to delete

let ary = [1, 2, 3, 4, 5, 6, 7, 8]
let res = ary.splice(0, 1)
console.log(res)
ary.splice(ary.length - 1, 1)
ary.splice(2, 1)
console.log(ary)
Copy the code

Output: arrays [1] and arrays [2, 3, 5, 6, 7]

Sort () : The method is used to sort the elements of an array

There are two parameters that I’m going to replace with a and b

If a is less than b, which should precede B in the sorted array, returns a value less than 0.

If a is equal to b, 0 is returned.

If a is greater than b, return a value greater than 0.

If there is no default parameter, sort by alphabetical order.

If the array contains numbers, the array is sorted by the number of digits in order from 0 to 9.

Let arr = [8,16,50,6,100,1] console.log(arr.sort())Copy the code

The output is 1,100,16,50,6,8

If you want it to sort by numeric size, you also use a sort function

Let arr = [8,16,50,6,100,1] console.log(arr.sort(sortNumber))Copy the code

The output is 1,6,8,16,50,100

The array is randomly shuffled

Let arr = [' zhang sanfeng ', 'xiao feng', 'no', 'zhang mowgli', 'jia baoyu', 'zhang fei']. Console. log(arr.sort(function () {return 0.5 - math.random ()}));Copy the code

Reduce () method

The reduce method takes two arguments. The first argument is a function that operates on an array item. The second parameter is the initial value passed in. The most common reduce method is stacking

It will iterate through the stack from the given value until all the items are added

var items = [10, 120, 1000]; // sumSoFar initializes first with 1, and item initializes first with 10; var total = items.reduce(function add(sumSoFar, item) { // 1 + 10 = 11 // 11 + 120 = 131 // 131 + 1000 = 1131 console.log(sumSoFar, item); return sumSoFar + item; // This value will become the next sumSoFar}, 1); console.log(total); / / 1131Copy the code

Subtract between arrays

<script> let arr1 = [ { id: 0, name: 'a' }, { id: 1, name: 'b' }, { id: 2, name: 'c' } ] let arr2 = [{ id: 1, name: 'b'}] // Loop over elements in arr1: // If the current element does not appear in arr2, it stays; // If the current element is present in arr2, discard it. Let array = arr1.filter((item1) => {// console.log(item1) // If arr1 does not appear in arr2, Return arr2.findIndex((item2) => Item1. id === Item2.id) === -1}) // arr1-arr2 =? console.log(array) </script>Copy the code

You can also use the some method to shorten it to a line of code

let arr1 = [ { id: 0, name: 'a' }, { id: 1, name: 'b' }, { id: 2, name: 'c' } ] let arr2 = [{ id: 1, name: 'b' }] return this.arr1.filter((item) => ! this.arr2.some(index => index.id === item.id))Copy the code

Output:

[{ id: 0, name: 'a' }, { id: 2, name: 'c' }]
Copy the code