Const arr = [1, 2, 3, 4, 5]Copy the code
1: push ()
- The push() method adds one or more elements to the end of an array and returns the array’s new length.
- Syntax: array name. push(element 1… , element N),
- Parameter: element 1… Element N, which represents the element added to the array, can be multiple or single elements separated by commas (,).
- Return value: When this method is called, the new length property value is returned
- Whether the original array is changed: true
- Es6 method: false
arr.push(12, 14) // 6
console.log(arr); // [1, 2, 3, 4, 5, 12, 14]
Copy the code
2: the pop ()
- The pop() method removes the last element from the array and returns it.
- Syntax: array name.pop ()
- Parameter: No parameter
- Return value: The deleted element, which is the last element of the original array
- Whether the original array is changed: true
- Es6 method: false
console.log(arr.pop()) // 14
console.log(arr) // [1, 2, 3, 4, 5, 12]
Copy the code
3: the shift ()
- The shift() method removes the first element from the array and returns the value of that element. This method changes the length of the array. In contrast to the pop ()
- Syntax: array name.shift ()
- Parameter: No parameter
- Return value: the deleted element, that is, the element value with index 0
- Whether the original array is changed: true
- Es6 method: false
console.log(arr.shift()) // 1
console.log(arr) // [2, 3, 4, 5, 12]
Copy the code
4: the unshift ()
- The unshift() method adds one or more elements to the beginning of an array, as opposed to push()
- Syntax: array name. unshift(element1,… , elementN)
- Parameters: Can be one or multiple parameters separated by commas (,)
- Return value: The new length of the array
- Whether the original array is changed: true
- Es6 method: false
console.log(arr.unshift(1)) // 6
console.log(arr) // [1, 2, 3, 4, 5, 12]
Copy the code
5: concat ()
- The concat() method is used to merge two or more arrays.
- Syntax: let variable name = array. Concat (parameter 1, parameter 2… Parameters n)
- Arguments: can be an array or a non-array
- Return value: a new array of arrays and parameters
- Whether the original array is changed: false
- Es6 method: false
- Description: is a shallow copy of the way
let newArr = arr.concat(['a', 'b', 'c'], 100, 200)
console.log('newArr ', newArr) // [1, 2, 3, 4, 5, 12, 'a', 'b', 'c', 100, 200]
Copy the code
6: Slice () array interception
- Meaning: The slice() method returns a new array object that is a shallow copy (including begin, but not end) of the array determined by begin and end.
- Syntax: let variable name = array slice(begin, end)
- Parameters: begin at which interception starts and end at which interception ends. End can be omitted. If omitted, the end is truncated by default. Begin must size end or return an empty array. The argument can also be negative, and the last item in the array is -1. Parameter Form attribute Left Close Right Open interval ([begin, end))
- Return value: Returns a new array
- Whether the original array is changed: false
- Es6 method: false
- Description: is a shallow copy of the way
Let arr2 = [1,2,3,4,5] console.log(arr2. Slice (3)) // [4, 5] console.log(arr2. Slice (3, 19) // 5] console.log(arr2.slice(-1)) // If the argument is negative, the last item of the array defaults to -1, So five the console output. The log (arr2. Slice (3, 1)) / / [3, 4] the console log (arr2. Slice (4, 4)) / / [] the console. The log (arr2. Slice (4, 2)) / / []Copy the code
7: indexOf ()
-
Meaning: Returns the first index where a given element can be found in the array, or -1 if none exists
-
Syntax: array name.indexof (searchElement, fromIndex)
-
Parameters:
SearchElement: the element to search for fromIndex: Where to start the search, starting from 0 by default. You can omit this parameterCopy the code
-
Return value: returns the first index if present, -1 otherwise
-
Whether the original array is changed: false
-
Es6 method: false
Console. log([1, 2, 3, 1].indexof (1)) // Returns 0, returns only the first index that meets the condition console.log([1, 2, 3, 1].indexof (10)) // returns -1, there is no 10 in the array, So I return -1Copy the code
8: splice()
- Method modifies an array by removing or replacing existing elements or adding new ones in place, and returns the modified contents as an array. This method changes the original array.
- Splice (start[, deleteCount[, item1[, item2[,…]]]])
- Parameters:
DeleCount: indicates the number of elements to be deleted. Item, items: indicates the new elements to be addedCopy the code
- Return value: Array of deleted elements
- Whether the original array is changed: true
- Es6 method: false
For example,
var arr = [1, 2, 3, 4, 5]
arr.splice(3, 2) // [4, 5]
console.log(arr); // [1, 2, 3]
arr.splice(0, 0, 6, 7, 8) // []
console.log(arr); // [6, 7, 8, 1, 2, 3]
Copy the code
9: the join ()
- Method concatenates all the elements of an array (or an array-like object) into a string and returns the string. If the array has only one item, that item is returned without a delimiter.
- Join ([separator])
- Parameters:
Separator: an array element is separated by a comma if it is omittedCopy the code
- Return value: a string composed of arrays
- Whether the original array is changed: false
- Es6 method: false
For example,
Var arr = [1, 2,3,4,5] console.log(arr.join()) // '1,2,3,4,5' console.log(arr.join('')) // '12345' But the separator is an empty string the console. The log (arr. Join (', ') / / '1, 2, 3, 4, 5'. The console log (arr. Join (' - ') / / 'the 1-2-3-4-5' console. The log ([1]. The join (', ')) // '1' If the array has only one item, that item is returned without a delimiter.Copy the code
-
Others: How does join() differ from toString()?
Join () can take arguments
ToString () has no arguments, and adding arguments will not work
10:
Continuously updated…