Array API methods in javascript

Array. Concat () : merges arrays

let arr1 = [1.2.3]
let arr2 = [4.5.6]
let arr = arr1.concat(arr2) 
console.log(arr) // [1, 2, 3, 4, 5, 6]
Copy the code

Array.indexof () : look for the elements in the array and find the returned index value. If not, return -1

let arr1 = [1.2.3]
let arr = arr1.indexOf(1) 
console.log(arr) / / 0
Copy the code

Array. Join () : joins an array

let arr1 = [1.2.3]
let arr = arr1.join(' ')
console.log(arr) / / '123'
Copy the code

Array.sort () : sort an array

let arr1 = [1.45.25.48.2.98.3]

// The sort method can only sort two digits by the size of the first digit, so it can pass a callback
let arr = arr1.sort(function(a, b) { return a - b }) // From small to large [1, 2, 3, 25, 45, 48, 98]
arr1.sort(function(a, b) { return b - a }) // From large to small [98, 48, 45, 25, 3, 2, 1]

// let arr = arr1.sort()
console.log(arr) // [1, 2, 25, 3, 45, 48, 98]

Copy the code

Array.isarray () : Determines whether it is an Array

let arr1 = [1.45.25.48.2.98.3]
let obj = { age: 22 }
console.log(Array.isArray(arr1)) // true
console.log(Array.isArray(obj)) // false
Copy the code

Array.from() : Array false to true

Array.foreach () : Iterates through the array

let arr1 = [1.45.25.48.2.98.3]
arr1.forEach((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    console.log(item) // 1 45 25 48 2 98 3
})
Copy the code

Array.find () : Iterates through the array (returns the first occurrence of a conditional element in the array)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.find((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item > 5
})
console.log(arr2) / / 45
Copy the code

Array. FindIndex () : Iterates through the array (returns the index value of the first occurrence of the element in the array that satisfies the condition; if not found, returns -1)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.findIndex((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item>5
})
console.log(arr2) / / 1
Copy the code

Array.some () : iterates through the array (if one of the elements in the array satisfies the condition, it is true, and the loop is terminated immediately)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.some((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item > 5
})
console.log(arr2) // true
Copy the code

Every () : iterates through the array (find elements in the array that satisfy the condition, true if they all do)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.every((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item >= 1
})
console.log(arr2) // true
Copy the code

Array.filter () : iterates through the array (filters array unit values and returns a new array)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.filter((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item > 5
})
console.log(arr2) // [45, 25, 48, 98]
Copy the code

Array.map () : Iterates through the array (iterates over the array to generate a new array)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.map((item, index, arr) = > {
    // item: each item in the array
    // index: the index value of each item in the array
    // obj: array itself
    return item * 5
})
console.log(arr2) // [5, 225, 125, 240, 10, 490, 15]
Copy the code

Array.reduce () : traverse the array (sum)

let arr1 = [1.45.25.48.2.98.3]
let arr2 = arr1.reduce((sum, item) = > {
    // sum: the sum of the elements in the array
    // item: each item in the array
    sum += item
    return sum
}, 0)
console.log(arr2) / / 222
Copy the code

Array.includes () : Searches for elements in the array. Returns true if found. Returns false if not

let arr1 = [1.45.25.48.2.98.3]
let arr = arr1.includes(45)
console.log(arr) // true
Copy the code

Array. Push () : Adds elements at the end of the array

let arr = ['Joe'.'bill']
arr.push('Cathy')
console.log(arr) // [" Zhang SAN ", "Li Si "," Wang Wu "]
Copy the code

Array. Unshift () : Adds elements to the array header

let arr = ['Joe'.'bill']
arr.unshift('Cathy')
console.log(arr) // [" Wang Wu ", "Zhang SAN "," Li Si "]
Copy the code

Array.pop () : Deletes the last element in the array

let arr = ["Zhang"."Bill"."Fifty"]
arr.pop('Cathy')
console.log(arr) // [" Zhang SAN ", "Li Si "]
Copy the code

Array.shift () : removes the first element in the array header

let arr = ["Zhang"."Bill"."Fifty"]
arr.shift('Cathy')
console.log(arr) // [" Li si ", "Wang Wu "]
Copy the code

Array.splice () : dynamically delete and add elements

let arr = ["Zhang"."Bill"."Fifty"]
arr.splice(0.1.'Cathy')
console.log(arr) // [" Wang wu ", "Li Si "," Wang wu "]
Copy the code