Article directory
- Preface
-
- 1. Stack methods: push(), pop()
- 2. Queue methods: unshift(), shift()
- 3.indexof()、lastIndexOf()、includes()
- 4. Operation methods: concat(), slice(), splice()
- 5.Array.isArray()
- 6. Sorting methods: sort(), reverse()
- 7. Conversion methods: toString(), join()
- 8.keys()、values()、entries()
- 9. Iteration methods: every(), some(), filter(), forEach(), map()
- 10. Static methods for creating arrays: from(), of()
- 11.reduce()
- Summarize
Preface
Recently, I have sorted out the common methods of JavaScript arrays, which will make it easier to review them in the future.
1. Stack methods: push(), pop()
push()
Add at the end (receives any number of parameters) Returns the latest length of the array
pop()
Delete at the end Returns the deleted item
2. Queue methods: unshift(), shift()
unshift()
Add at the beginning Return the new array length
shift()
Delete at the beginning (delete the first item of the array) Return the first element deleted
3.indexof()、lastIndexOf()、includes()
indexof()
Search from front to back. If it exists, return the index number. If it does not exist, return -1.
lastIndexof()
Search from back to front. If it exists, return the index number. If it does not exist, return -. 1
includes()
Returns a Boolean value indicating whether an item matching the specified element was found.
4. Operation methods: concat(), slice(), splice()
concat()
Creates a new array based on all elements of an existing array. You can pass multiple parameters and add them to the end of the array.
slice()
Create a new array containing one or more elements in the original array (interception operation)
This operation does not affect the original array!
Receives one or 2 parameters and returns the start index and end index of the element.
- 1 parameter: Returns all elements from the starting index to the end of the array.
- 2 parameters: Returns all elements from the start index to the end index, excluding the end index.
Note: The slice() parameter has a negative value, array length + negative value = determine position
Eg: An array of 5 elements, Slice(-2,-1) is equivalent to slice(3,4)
If the end position is less than the start position, an empty array is returned!
splice()
Always returns an array containing the deleted elements in the array; if no elements are deleted, an empty array is returned.
-
Delete passes 2 parameters (the position of the first element to be deleted, the number to be deleted) and returns the deleted elements
-
Insertion passes 3 parameters (starting position, 0, element to be inserted). Multiple elements can be inserted.
-
Replace and pass 3 parameters (starting position, number of elements to be deleted, any number of elements to be inserted)
5.Array.isArray()
Determine whether it is an array, return true if it is, otherwise return false
6. Sorting methods: sort(), reverse()
sort()
Sort the array in ascending order by default
reverse()
Sort the array elements in reverse order
7. Conversion methods: toString(), join()
toString()
Converts the array to a string, separating each item with a comma
join(‘字符串分割符’)
Converts the array to a string, splitting each item with a comma; returns a string containing all items.
Note: If no parameters are passed to join(), or undefined is passed, commas are still used as delimiters.
8.keys()、values()、entries()
keys()
Returns an iterator of array indexes
values()
Returns an iterator of array elements
entries()
Returns an iterator of array index/value pairs
These methods all return iterators, so their contents can be directly converted into array instances through Array.from().
9. Iteration methods: every(), some(), filter(), forEach(), map()
every()
Run the passed function on each item of the array. If the function returns true for each item, this method returns true.
some()
Run the passed function on each item of the array. If one function returns true, then this method returns true.
filter()
Run the passed function on each item of the array. The items that the function returns true will be composed into an array and returned.
forEach()
Run the passed function on each item in the array. Note: forEach() has no return value.
The callback function in the forEach method has 3 parameters: array element, array index, and array itself.
map()
Run the passed function on each item in the array, returning a function composed of the results of each function call.
Requirement: Each element in the array arr must be multiplied by 2.
Requirement: Add 1 to the id of each object in the array.
Requirement: Synthesize the id and name in the object into new arrays.
10. Static methods for creating arrays: from(), of()
from()
Used to convert an array-like structure into an array instance
First parameter (required): an array-like object, that is, any iterable structure
The second parameter (optional): mapping function parameter (this function can directly enhance the value of the new array)
The third parameter (optional): used to specify the mapping function the value of this.
Pseudo array has length attribute:
of()
is used to convert a set of parameters into an array instance
11.reduce()
Traverse from the first item of the array to the last item of the array
Receives two parameters (the merge function will be run for each item, using it as the initial value of the merge starting point )
The callback function receives 4 parameters:
1. prev is the return value of the accumulator callback; represents the return value when the callback was last called, or the initial value init;
2. cur is the currently processed element in the array
3. index is the index of the current element (optional)
4. array The array in which the current element is located (optional)
Summarize
The above is the content of the array method that I want to share today. Everyone is welcome to come and share more useful methods~