@TOC

join()

Join (” “); insert string (” “) in parentheses (” to quote “) example:

let arr = [1.2.3];
console.log(arr.join());/ / 1, 2, 3
console.log(arr.join("-"));/ / 1-2-3
Copy the code

Push () and pop ()

Push () : Example of adding one or more elements to the end of an array and returning a new length:

let arr = [1.2.3] 
arr.push(4)
arr.push(5.6)
console.log(arr)  / / [6]
Copy the code

Example of pop() : used to delete the last element of an array and return the deleted element:

let arr = [1.2.3.4.5.6] 
arr.pop()
console.log(arr)  / / [1, 2, 3, 4, 5]
Copy the code

The shift () and the unshift ()

Shift () : Used to remove the first element from an array and return the value of the first element example:

let arr = [1.2.3.4] 
const num = arr.shift()
console.log(num)  / / 1
console.log(arr)  / / / 2 and 4
Copy the code

Unshift () : Can add one or more elements to the beginning of an array and return a new length example:

let arr = [1.2.3] 
arr.unshift(0)
arr.unshift(-2, -1)
console.log(arr)  / / /,0,1,2,3-2-1
Copy the code

concat()

Concat () : Example of joining two or more arrays:

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

sort()

Example of sort() : used to sort the elements of an array:

let arr = [3.7.6.9.1.5.8.4.2]
arr.sort()          // Defaults from small to large
console.log(arr)    // [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr.sort((a,b) = >{  // From large to small
   return b - a
})
console.log(arr)    // [9, 8, 7, 6, 5, 4, 3, 2, 1]
Copy the code

reverse()

Reverse () : Example of an array used to reverse the order of elements:

let arr = [1.2.3.4]
arr.reverse()          // Defaults from small to large
console.log(arr)    / /,3,2,1 [4]
Copy the code

slice()

Slice () : Returns a new array of items from the specified start index to the end index, without modifying the original array, and can take one or two arguments. Example:

let arr = [1.2.3.4.5.6.7]
let arr1 = arr.slice(1)   
let arr2 = arr.slice(1.4)
let arr3 = arr.slice(1, -2)
let arr4 = arr.slice(-4, -2)
console.log(arr)     // [1, 2, 3, 4, 5, 6, 7] The original array remains unchanged
console.log(arr1)    // [2, 3, 4, 5, 6, 7]
console.log(arr2)    //  [2, 3, 4]
console.log(arr3)    // [2, 3, 4, 5]
console.log(arr4)    //  [4, 5]
// arr1 sets only one parameter, 1, so the array is returned from subscript 1 (including 1) to the end of the array
// arr2 sets parameters 1, 4, so the array is returned from index 1 (inclusive) to index 4 (excluding 4)
// arr3 sets the arguments 1, -2. When a negative argument occurs, the negative number is replaced by the array length value (7 in this case), so the array returned is the index from 1 (inclusive) to 5 (excluding 5)
// Arr4 sets the parameters -4, -2, negative (algorithm above), so the array returned is from the position of index 3 (including 1) to the array of index 5 (excluding 5)
Copy the code

splice()

Splice () : used to add, delete, or replace elements in an array. Add: Any number of items can be inserted into a specified position by providing three arguments: the starting position,0 (number of items to delete), and the item to be inserted.

Delete: Specify two parameters: the location of the first item to delete and the number of items to delete.

Replace: You can insert any number of items into a specified location and delete any number of items simultaneously by specifying three parameters: the start location, the number of items to delete, and any number of items to insert. The number of inserted items need not be the same as the number of deleted items.

Example:

/ / add
let arr = [1.2.3.4.5]
arr.splice(1.0.8.9)  
console.log(arr)    // [1, 8, 9, 2, 3, 4, 5]
/ / delete
let arr1 = [1.2.3.4.5]
arr1.splice(1.2)        
console.log(arr1)    //  [1, 4, 5]
/ / replace
let arr2 = [1.2.3.4.5]
arr2.splice(1.2.6.7)
console.log(arr2)    // [1, 6, 7, 4, 5]
Copy the code

IndexOf () and lastIndexOf ()

IndexOf () : Returns the location of a specified element in an array, taking two arguments, the first one item (must. The element to look for), the second argument start (an optional integer argument specifying where to start the search in the array) example:

let arr = [1.2.3.4.5.3]
let index1 = arr.indexOf(1)
let index2 = arr.indexOf(2.2)
let index3 = arr.indexOf(3.3)
let index4 = arr.indexOf(3.1)
console.log(index1) // 0 finds element 1 in arr, returns the subscript position of the array
console.log(index2) // -1 looks for element 1 in the array arr, and looks backwards from the array subscript 2
console.log(index3) // 5 looks for element 3 in the array arr, and looks back from the array subscript 1. If found, returns the element's first occurrence at subscript 5
console.log(index4) // 2 Same principle as above
Copy the code

LastIndexOf () : Returns the last occurrence of a specified element in the array, taking two arguments, the first item (must. The element to look for), the second argument start (an optional integer argument specifying where to start the search in the array) example:

let arr = [1.2.3.4.5.3]
let index1 = arr.lastIndexOf(3)
let index2 = arr.lastIndexOf(3.1)
let index3 = arr.lastIndexOf(3.4)
let index4 = arr.lastIndexOf(3.5)
console.log(index1) // return the last occurrence of element 3 in the array arr
console.log(index2) // -1 looks for element 3 in arr, and looks forward from the array subscript 1
console.log(index3) // 2 returns the first occurrence of the element at subscript 2 in arr, starting at subscript 4
console.log(index4)  // 5 Same principle as above
Copy the code

includes

Includes: checks whether an array contains a specified value, returning true if so, false otherwise example:

let arr = ['one'.'two'.'three'.'four'.'five']
let flag = arr.includes('one')
let flag1 = arr.includes('six')
let flag2 = arr.includes('two'.1)
let flag3 = arr.includes('two', -1)
console.log(flag)  // true 
console.log(flag1) // false
console.log(flag2) // true
console.log(flag3) // falseReturn a Boolean parameter:1.Necessary, the element to find2.Not necessary, array subscript position to start searchCopy the code

forEach

ForEach: Example of iterating through an array and passing elements and subscripts to a callback function:

let arr = [1.2.3]
arr.forEach((v, i) = > {
  console.log(v)   // each element in 1, 2, 3 v array
  console.log(i)   // The index of each element in the array 0, 1, 2, I}) Description: No return valueCopy the code

map

Map () : Iterates through an array and returns a new array with the values of the elements in the original array. Example:

let arr = [1.2.3]
let newArr = arr.map((v) = > {
      return v += 1
})
console.log(arr)    / / [1, 2, 3]
console.log(newArr) / / [2, 3, 4]Description: Returns a new array without modifying the original arrayCopy the code

filter

Filter: Iterate over an array to create a new array whose elements meet the filter criteria example:

let arr = [1.2.3.4]
let newArr = arr.filter((v) = > {
      return v > 2  // Returns an array element greater than 2
})
console.log(arr)    // [1, 2, 3, 4]
console.log(newArr) / / [3, 4]Description: Returns a new array without modifying the original arrayCopy the code

every

Every: iterates through the array to determine whether each item in the array meets the condition. This returns true only when all items meet the condition. Example:

let arr = [1.2.3.4]
let flag = arr.every((v) = > {
      return v > 2  // Determine if the element is greater than 2
})
console.log(arr)    // [1, 2, 3, 4]
console.log(flag)   Not all elements in arr are greater than 2, so return falseDescription: Returns a Boolean value without modifying the original arrayCopy the code

some

Some: Iterates through the array to determine whether each item in the array satisfies the condition, and returns true if only one item satisfies the condition. Example:

let arr = [1.2.3.4]
let flag = arr.some((v) = > {
      return v > 2  // Determine if the element is greater than 2
})
console.log(arr)    // [1, 2, 3, 4]
console.log(flag)   // true There are elements in arr greater than 2, so return trueDescription: Returns a Boolean value without modifying the original arrayCopy the code