Those commonly used array methods in JS:

Array add, delete, change, query:

Push (insert element < pass more than one >)

Inserts an element to the end of an array

let arr = [1, 2, 3]

arr.push(4, 5)

letRes = arr.push(4, 5) // Returns the length of the array after insertion (res: 5)

console.log(arr) // [1, 2, 3, 4, 5]

Copy the code

Unshift (insert elements < multiple >)

Inserts an element into the head of an array

let arr = [1, 2, 3]

arr.unshift(-2, -1, 0)

letRes = arr.unshift(-2, -1, 0) // Return the length of the array after the insertion (res: 6)

console.log(arr) // [-2, -1, 0, 1, 2, 3]

Copy the code

pop()

Deletes the last element of an array

let arr = [1, 2, 3]

arr.pop()

letRes = arr.pop() // Returns the deleted element (res: [3])

console.log(arr) // [1, 2]

Copy the code

shift()

Deletes the first element of an array

let arr = [1, 2, 3]

arr.shift()

letRes = arr.shift() // Returns the deleted element (res: [1])

console.log(arr) // [2, 3]

Copy the code

Slice (subscript of start element, subscript of end element)

Action: Intercepts a specific element of an array (does not change the original array, can be used as a shallow copy)

/ / shallow copy

let arr = [1, 2, 3]

letNewArr = arr.slice() // Returns a truncated new array

console.log(newArr) // [1, 2, 3] 



// Intercepts the element

let arr = [1, 2, 3]

letNewArr = arr.slice(0, 2) // Slice from element 0 to element 2

console.log(newArr) // [1, 2]

Console. log(arr) // [1, 2, 3] // does not change the original array

Copy the code

Splice (subscript of start element, number of elements removed, elements added…)

Function: You can add or delete elements

// Add a new element

let arr = [1, 3, 5]

Arr.splice (1, 0, 2) // Add an element 2 after the element subscript 1

letRes = arr.splice(1, 0, 2) // Empty array [] (res: [])

console.log(arr) // [1, 2, 3, 5]



// Delete elements

let arr = [1, 2, 3]

Arr.splice (1, 1) // Deletes an element after the element subscript 1

letRes = arr.splice(1, 1) // Return value for deleted element (res: [2])

console.log(arr) // [1, 3]

Copy the code

Join (pass in a flag to split each element)

Function: Converts an array to a string

let arr = ['zbw'.'cyl']

let res = arr.join(', '

console.log(res) // 'zbw, cyl'



let res = arr.join(The '-')

console.log(res) // 'zbw-cyl'

Copy the code

concat()

Joins multiple arrays or values

let arr1 = [1, 2, 3]

let arr2 = [4, 5, 6]



let res = arr1.concat(arr2)

console.log(res) // [1, 2, 3, 4, 5, 6]



let res = arr1.concat(4, arr2, 7, 8)

console.log(res) // [1, 2, 3, 4, 4, 5, 6, 7, 8]

Copy the code

Higher-order functions that operate on arrays

Higher-order functions: Functions that pass in a function as an argument or return a function as a value are higher-order functions

map()

Function: Operates on an array and returns a processed array

let arr = [1, 2, 3, 4]

let res = arr.map(item => item * 2)

console.log(res) // [2, 4, 6, 8]

Copy the code

filter()

Function: Filter an array and return elements whose operation expression is true

let arr = [1, 2, 3, 4]

let res = arr.filter(item => item >= 2)

console.log(res) // [2, 3, 4]

Copy the code

Reduce (pre< initial value >, cur< current item >, ARR < array itself >, < initial value >)

Function: sum, array to object

/ / sum

let arr = [1, 2, 3, 4]

let res = arr.reduce((pre, cur) => {

  return pre += cur

}, 0) // This is summation so the initial value is 0

console.log(res) // 10



// Array to object

let arr = [

  { id: 1, name: 'zbw', age: 18 },

  { id: 2, name: 'cyl', age: 18 },

]



let obj = arr.reduce((pre, cur) => {

   pre[cur.id] = cur.name

   return pre

}, {}) // This is array to object, so the initial value is empty object {}



console.log(obj) // {1: 'zbw', 2: 'cyl'

Copy the code

forEach()

What forEach() does is similar to what map() does except that forEach() changes the array and returns no value

let arr = [

  { name: 'zbw', age: 22 },

  { name: 'cyl', age: 18 },

]



arr.forEach(item => item.age = 20)

console.log(arr) // [{ name: 'zbw', age: 20 },{ name: 'cyl', age:20 }]

Copy the code

some()

Returns true if one of the items in the array matches a conditional expression, false otherwise

let arr = [1, 2, 3, 4]

let bool = arr.some(item => item > 2)

console.log(bool) // trueBecause there are elements greater than 2



let bool = arr.some(item => item > 5)

console.log(bool) // falseBecause there's no element greater than 5

Copy the code

every()

Returns true if all elements in an array match the expression condition, false otherwise

let arr = [1, 2, 3, 4]

let bool = arr.every(item => item > 0)

console.log(bool) // trueBecause everything is greater than 0



let bool = arr.every(item => item > 1)

console.log(bool) // falseBecause there are elements that are not greater than 1

Copy the code

find()

Finds the first element in an array that matches the condition

let arr = [{ id: 1, name: 'zbw', age: 18 }, { id: 2, name: 'cyl', age: 18 }]



let res = arr.find(item => item.age === 18)

console.log(res) // { id: 1, name: 'zbw', age: 18 }



let res = arr.find(item => item.id === 2)

console.log(res) // { id: 2, name: 'cyl', age: 18 }

Copy the code