This is the 20th day of my participation in the August More Text Challenge
Two, interception splicing
2.1 slice
The slice() method returns a new array object that is a shallow copy of the original array determined by begin and end. The original array will not be changed.
arr.slice([begin[, end]])
Note: The copied array contains the start subscript element, not the end subscript element
@params
By default, end is copied to the end of the array. You can use negative values to select elements from the end of the array.@return
: returns the copied subarrayWhether to change the original array
: don’t change
var arr = [1.2.3.6.7.8];
var res = arr.slice(1.4);
console.log(res); / /,3,6 [2]
console.log(arr); / /,2,3,6,7,8 [1]
Copy the code
2.2 the concat
The concat() method is used to merge two or more arrays. This method does not change an existing array, but returns a new array.
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
@params
: multiple arbitrary items, which can be arrays or single items@return
: Returns the new array after the mergeWhether to change the original array
: don’t change
var arr = [1.2.3];
var res = arr.concat(1[0.0]);
console.log(res); / /,2,3,1,0,0 [1]
console.log(arr); / / [1, 2, 3]
Copy the code
Third, find judgment
3.1 includes
The includes() method is used to determine whether an array contains a specified value, returning true if it does and false otherwise.
arr.includes(valueToFind[, fromIndex])
@params
: Parameter 1 is mandatory, indicating the element to be queried. Parameter 2 is optional, indicating that the element to be queried starts from the specified position. (If the value is negative, the value is set to 0 if the negative value exceeds the length of the array.)@return
: Returns a Boolean valueWhether to change the original array
: don’t change
var arr = [1.2.3];
var res = arr.includes(2);
console.log(res); // true
console.log(arr); / / [1, 2, 3]
Copy the code
3.2 the find
The find() method returns the value of the first element in the array that satisfies the provided test function. Otherwise return undefined.
arr.find(function(item, index, array){}, thisArg)
@params
ThisAr represents the object to which this points when the callback is executed@return
: The first element or undefined that satisfies the conditionWhether to change the original array
: don’t change
const array = [5.12.8.130.44];
const found = array.find(element= > element > 10);
console.log(found); / / 12
console.log(array); // [5, 12, 8, 130, 44]
Copy the code
3.3 findIndex
The findIndex() method returns the index of the first element in the array that satisfies the provided test function. Returns -1 if no corresponding element is found.
arr.findIndex(callback[, thisArg])
@params
ThisAr represents the object to which this points when the callback is executed@return
: the first index that meets the condition, or -1Whether to change the original array
: don’t change
const array = [5.12.8.130.44];
const found = array.findIndex(element= > element > 10);
console.log(found); / / 1
console.log(array); // [5, 12, 8, 130, 44]
Copy the code
3.4 indexOf
The indexOf() method returns the first index in the array where a given element can be found, or -1 if none exists.
arr.indexOf(searchElement[, fromIndex])
@params
: Mandatory: indicates the elements to be queried. Optional: indicates that the elements to be queried start from the specified location@return
: Returns the index of the search item if it exists, or -1 if it does notWhether to change the original array
: don’t change
var arr = [1.2.3];
console.log(arr.indexOf(2)); / / 1
console.log(arr.indexOf(8)); // -1
Copy the code
3.5 lastIndexOf
The lastIndexOf() method returns the index of the last element in the array specified (that is, a valid JavaScript value or variable), or -1 if none exists. Look forward from the back of the array, starting at fromIndex.
arr.lastIndexOf(searchElement[, fromIndex])
@params
: Mandatory: indicates the elements to be queried. Optional: indicates that the elements to be queried start from the specified location@return
: Returns the index of the search item if it exists, or -1 if it does notWhether to change the original array
: don’t change
var arr = [1.2.3.2.2.3.4];
console.log(arr.lastIndexOf(3)); / / 5
Copy the code