Common ways to organize JavaScript arrays


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()

Insert image description here
push() Add at the end (receives any number of parameters) Returns the latest length of the array
Insert image description here
pop() Delete at the end Returns the deleted item
Insert image description here

2. Queue methods: unshift(), shift()

unshift() Add at the beginning Return the new array length
Insert image description here
shift() Delete at the beginning (delete the first item of the array) Return the first element deleted
Insert image description here

3.indexof()、lastIndexOf()、includes()

indexof() Search from front to back. If it exists, return the index number. If it does not exist, return -1.
Insert image description here
lastIndexof() Search from back to front. If it exists, return the index number. If it does not exist, return -. 1
Insert image description here
includes() Returns a Boolean value indicating whether an item matching the specified element was found.
Insert image description here

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.
Insert image description here
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.
    Insert image description here

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)
Insert image description here
If the end position is less than the start position, an empty array is returned!
Insert image description here
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
    Insert image description here

  • Insertion passes 3 parameters (starting position, 0, element to be inserted). Multiple elements can be inserted.
    Insert image description here

  • Replace and pass 3 parameters (starting position, number of elements to be deleted, any number of elements to be inserted)
    Insert image description here

5.Array.isArray()

Determine whether it is an array, return true if it is, otherwise return false
Insert image description here

6. Sorting methods: sort(), reverse()

sort() Sort the array in ascending order by default
reverse() Sort the array elements in reverse order
Insert image description here

7. Conversion methods: toString(), join()

toString() Converts the array to a string, separating each item with a comma
Insert image description here
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.
Insert image description here

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().
Insert image description here

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.
Insert image description here
some()Run the passed function on each item of the array. If one function returns true, then this method returns true.
Insert image description here
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.
Insert image description here
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.
Insert image description here
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.
Insert image description here
Requirement: Add 1 to the id of each object in the array.
Insert image description here
Requirement: Synthesize the id and name in the object into new arrays.
Insert image description here

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.
Insert image description here
Pseudo array has length attribute:
Insert image description here
Insert image description here
of() is used to convert a set of parameters into an array instance
Insert image description here

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)
Insert image description here
Insert image description here


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~

Guess you like

Origin blog.csdn.net/m0_52043522/article/details/130343062