“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Previous we introduced some methods will change array itself Chen students の front-end notes | about an array of some commonly used methods are (a), this issue we will continue to introduce other commonly used methods about array.

Does not change the method of the original array

slice()

Definition: The slice() method returns a new array that is a shallow copy of the original array determined by begin and end.

Grammar: Array. Prototype. Slice (begin and end)

Parameters:

  1. Begin (Optional) : The default value is 0, indicating that the array elements are extracted from the index.
  2. End (Optional) : The default value is the length of the array. It stops extracting array elements until the index is reached. [begin,end)

Use:

let arr = [1.2.3.4.5]

let arr1 = arr.slice(1)
console.log('Array arr1:',arr1)    // [2, 3, 4, 5]
let arr2 = arr.slice(1.3)
console.log('Array arr2:',arr2)    / / [2, 3]
let arr3 = arr.slice()
console.log('Array arr3:',arr3)    // [1, 2, 3, 4, 5]

// Test shallow copy
let testCopyArr = [1.2, {name:'Mr. Chen?'},3.4]
let cpArr = testCopyArr.slice(0.3)
testCopyArr[0] = 99
testCopyArr[2].name = 'Test'
console.log('Changed array cpArr:',cpArr)
// 0: 1
// 1: 2
// 2: {name: 'Test'}
Copy the code

concat()

Definition: The concat() method is used to merge two or more arrays and return the new array.

Grammar: Array. Prototype. Concat (item1,… ,itemN)

Parameters:

  1. Items: Values/arrays used for merging.

Use:

let arr1 = [1.2.3]
let arr2 = [4.5.6]
let arr3 = arr1.concat(arr2)
console.log('Array arr3:',arr3) // [1, 2, 3, 4, 5, 6]

let arr4 = arr3.concat(arr1,arr2,'? '.'! '.Awesome!)
console.log('Array arr4:',arr4) / / [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, '? ', '. ', 666]
Copy the code

join()

Definition: The join() method joins all the elements in an array into a string and returns the string, which can be specified.

Grammar: Array. Prototype. Join (separator)

Parameters:

  1. Separator (Optional) : Specifies a string to separate each element in the array. The default is “,”.

Use:

let arr = [1.2.3.4.5]

let str = arr.join()
console.log('Connected string STR:',str)      / / 1, 2, 3, 4, 5
let str1 = arr.join(The '#')
console.log('Connected string str1:',str1)    / / 1 # 2 # 3 # 4 # 5
let str2 = arr.join('abc')
console.log('Concatenated string str2:',str2)    // 1abc2abc3abc4abc5
Copy the code

indexOf()

Definition: The indexOf() method returns the first index in the array where a given element can be found, or -1 if none exists.

Grammar: Array. Prototype. IndexOf (value, fromIndex)

Parameters:

  1. Value (Mandatory) : indicates the element to be searched.
  2. FromIndex (Optional) : Start the search. The default is 0, or -1 if greater than or equal to the array length.

Use:

let arr = [1.2.1.4.1.NaN]
let pos1 = arr.indexOf(1)
let pos2 = arr.indexOf(1.1)
let pos3 = arr.indexOf(1.10)
let pos4 = arr.indexOf(NaN)        // NaN cannot be recognized
console.log(pos1,pos2,pos3,pos4)   // 0 2-1-1
Copy the code

lastIndexOf()

Definition: The lastIndexOf() method returns the last index in the array where a given element can be found, or -1 if none exists. (Search from back to front)

Grammar: Array. The prototype. The lastIndexOf (value, fromIndex)

Parameters:

  1. Value (Mandatory) : indicates the element to be searched.
  2. FromIndex (Optional) : Position to start the reverse lookup. The default array length is -1.

Use:

let arr = [1.2.1.4.1.NaN.2]
let pos1 = arr.lastIndexOf(2)
let pos2 = arr.lastIndexOf(2.5)
let pos3 = arr.lastIndexOf(2.10)
let pos4 = arr.lastIndexOf(NaN)
console.log(pos1,pos2,pos3,pos4)    // 1
Copy the code

includes()

Defines the: includes() method to determine if an array contains a specified value, returning true if it does and false otherwise.

Grammar: Array. Prototype. Includes (value, fromIndex)

Parameters:

  1. Value (Mandatory) : indicates the element to be determined.
  2. FromIndex (optional) : start the search. The default value is 0.

Use:

let arr = [1.2.'Mr. Chen?'.NaN]
console.log(arr.includes('Mr. Chen?'))   // true
console.log(arr.includes(1.1))           // false
console.log(arr.includes(NaN))           // true identifies NaN
Copy the code

conclusion

The next installment will cover some array traversal methods.

Welcome to point out any mistakes!