What is an array?

Is a collection of data used to store multiple values in a single variable

1. Array.isArray—- Checks whether the Array is an Array

Used to check whether the value passed is an array (method provided in H5)

Example:

2. Splice ()—- Add/delete

Add/remove elements from an array, returning the deleted element (changing the original array)

Grammar:

var arr = [1.2.3.4]
arr.splice(value,num,item...)
Copy the code

Parameters:

Value: Specifies the position to add/remove elements. Negative numbers start from the left of the end of the array

Num: Specifies the number of elements to be deleted. This parameter is mandatory

Item: Optional element to be added

The code is as follows:

3. The reduce () – accumulator

This method is used to sum up each element in the array and return the sum of the results.

Grammar:

array.reduce(callback(total, currentValue, currentIndex, arr), initialValue)
Copy the code

Parameters:

Callback: Mandatory, executes the function

Total: Mandatory, initial value, or returned value after calculation

CurrentValue: Mandatory. The current element

CurrentIndex: Optional, index of the current element

Arr: Optional array object to which the current element belongs

InitialValue: optional initialValue passed to the function

Code examples:

The arrow function can also be implemented as a normal function, where the return value is received as a variable, if it is written inside the function, it simply returns

Note: Reduce () does not perform a callback for an empty array.

4.every()

Checks whether all elements of an array match the specified condition

1. If an element is detected that does not meet the criteria, the entire expression returns false, and the rest of the elements are not tested;

2. Return true if all elements meet the criteria

Note:

1. Every () does not check for empty arrays.

2. Every () does not change the original array

Grammar:

array.every(function(currentValue,index,arr), thisValue)
Copy the code

Parameters:

CurrentValue: Specifies the value of the current element

Index: Optional, the index value of the current element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code examples:

5. The filter () – filtration

Detects array element filtering and returns a new array of all elements that match the criteria.

Note:

  1. Filter () does not detect an empty array
  2. It doesn’t change the original array
  3. The qualifying element returns a new array

Grammar:

array.filter(function(currentValue,index,arr), thisValue)
Copy the code

Parameters:

Function: Mandatory

CurrentValue: Mandatory, value of the current element

Index: Optional, index of an element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code examples:

6.findIndex()

Returns the position of the first element of the array passed in to test the condition.

Note that the findIndex() method calls the function once for each element in the array:

  • Returns when an element in an array is tested for a conditiontrueFindIndex () returns the index position of the element that matches the condition, and the subsequent value is not called.
  • Returns -1 if no element matches the criteria

 

Note: findIndex() does not execute for an empty array.

Note that findIndex() does not change the original value of the array.

Grammar:

array.findIndex(function(currentValue, index, arr), thisValue)
Copy the code

Parameters:

Function: Mandatory

CurrentValue: Mandatory, value of the current element

Index: Optional, index of an element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code examples:

find()Method that returns the value of the element found in the array rather than its index.

7. ForEach ()—-

Used to iterate over a number of elements, with no return value

Note that forEach() does not perform callbacks on empty arrays.

Grammar:

array.forEach(function(currentValue, index, arr), thisValue)
Copy the code

Parameters:

Function: Mandatory

CurrentValue: Mandatory, value of the current element

Index: Optional, index of an element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code sample

8. Push () added at the end of —-

Adds one or more elements to the end of the array and returns the new length.

Code examples:

9. Reverse () —- reverses arrays

Used to reverse the order of elements in an array

Code sample

10.sort () —- bubble sort

Used to sort the elements of an array

Grammar:

array.sort(sortfunction)
Copy the code

Parameters:

Sortfunction: Optional. Specify the sort order – ascending – descending. It has to be a function

Code examples:

ascending

Descending order

11. Join ()—- array to string

Use to convert all elements of an array to a string

Grammar:

array.join(separator)
Copy the code

Parameters:

Separator: Optional. Specifies the delimiter to use. If this parameter is omitted, a comma is used as a delimiter.

Return value: a string

Code examples:

12. Some ()

Checks whether an element in an array meets a specified condition

Note:

  • If one element satisfies the condition, the expression returnstrueThe remaining elements will not be tested.
  • If no element satisfies the condition, false is returned.
  • Some () does not check for empty arrays.
  • Some () doesn’t change the original array.

Grammar:

array.some(function(currentValue,index,arr),thisValue)
Copy the code

Parameters:

Function: Mandatory

CurrentValue: Mandatory, value of the current element

Index: Optional, index of an element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code examples:

The difference between some and every:

Every () :

Return false whenever one element does not meet the criteria;

Some () :

Return true as long as one element meets the criteria

13. Map () —- returns what is written in the function body

You specify a function to process an array and return the result as a new array

Note:

  • The original array is not changed, but the result is returned as a new array
  • Map () does not detect empty arrays.

Grammar:

The map syntax allows the callback to take three arguments. The second argument to map specifies this for the callback

array.map(function(currentValue,index,arr), thisValue)
Copy the code

Parameters:

Function: Mandatory

CurrentValue: Mandatory, value of the current element

Index: Optional, index of an element

Arr: Optional array object to which the current element belongs

ThisValue: Optional, object is used when the callback is executed, passed to the function, and is used as the value of “this”. If thisValue is omitted, the value of “this” is” undefined”

Code examples:

14.Set object —- array deduplication

Code examples:

15.slice()

Used to extract a portion of a string and return the extracted portion as a new string

Note: The slice() method does not alter the original array.

Grammar:

array.slice(start, end)
Copy the code

Arguments: start: Optional, from where, negative from the tail, including the last one

End: Select several

Code examples:

16.includes()

The includes() method is used to determine whether a string contains a specified substring. Returns true if a matching string is found, false otherwise.

Note: the includes() method is case sensitive.

grammar

string.includes(str, index)
Copy the code

The parameter value

STR: required, string to look for.

Index: Optional, set the search to start at that location. Default is 0.

The return value

Returns true if a matching string is found, false otherwise.

Happyend:

The above is often used in the array method, if there is insufficient, also please comment correct