This is the 15th day of my participation in the More Text Challenge. For details, see more text Challenge
This is a summary of some of the most common methods used in JavaScript arrays, classified by adding, deleting, and such purposes. It covers most of the everyday development scenarios.
increase
Push – Tail-add
Add one or more elements to the end, returning the new length of the array.
Unshift – Header added
Adds one or more elements to the header and returns the new length of the array.
delete
Pop – Tail deletion
Removes an element from the tail and returns the element.
Shift – Head removed
Removes an element from the header and returns the element.
change
Splice – Add Replace delete
Add, remove, and replace elements. It’s going to change the array.
arrayObject.splice(index,howmany,item1,..... ,itemX)Copy the code
check
Find – Find by function
Find the first element that satisfies the condition of the detection function and return that element, or undefined if not found.
FindIndex – Search by function
The first element that satisfies the condition of the detection function is found and the index of that element is returned. Cannot find return -1.
IndexOf – Searches by element value
Finds the element and returns the element index.
Cannot find return -1
The second parameter indicates the starting position of the search
LastIndexOf – Finds by element value
Look for elements from back to front and return the element index. If not found, return -1.
The second parameter indicates the starting position of the search
The sorting
Sort sort –
Sort the elements of an array. Note that the array is sorted on top of the original array. No copies are made.
If this method is called without arguments, the elements in the array are sorted alphabetically or, more precisely, by character encoding.
If you want to sort by other criteria, you need to provide a comparison function.
Reverse order
Invert the array and return the new array. It’s going to change the array.
merge
Join – Join into a string
Merges all elements of the array into a string and returns.
Concat – Merges into an array
Combine two or more arrays to return a completely new array.
traverse
forEach
Iterates over the elements in the array and executes the callback function.
array.forEach(function(currentValue, index, arr), thisValue)
Copy the code
Example:
let arr = ['a','b','c']
arr.forEach(function(currentValue, index, arr){
console.log('currentValue',currentValue)
console.log('index',index)
console.log('arr',arr)
})
Copy the code
detection
The value includes detection
Determines whether the string contains the specified substring. Return true if a matching string is found, false otherwise.
string.includes(searchvalue, start)
Copy the code
Find if the string contains Axjy
Look up from the sixth index position
Some – Functions contain checks
Detecting whether there are elements in the array can be verified by the detection function, which executes each element of the array in turn.
- If one of the elements satisfies the condition, the expression returns true, and the remaining elements are not tested
- If there are no elements that satisfy the condition, false is returned.
array.some(function(currentValue,index,arr),thisValue)
Copy the code
Every – The function is fully detected
Check whether all elements in the array can be validated by the check function.
- If one element in the array is detected as unsatisfactory, the entire expression returns false and the remaining elements are not tested.
- Returns true if all elements meet the criteria.
array.every(function(currentValue,index,arr), thisValue)
Copy the code
other
Filter – The filter function
Creates a new array by checking all the elements in the specified array that meet the criteria. It doesn’t change the original array.
array.filter(function(currentValue,index,arr), thisValue)
Copy the code
Slice – slice an array
The begin and end parameters are used to cut the array without changing the original array.
Map – map
Returns a new array whose elements are the values of the original array elements.
array.map(function(currentValue,index,arr), thisValue)
Copy the code
Returns an array with twice as many elements as the original array.
CopyWithin – Internal copy
Use to copy elements from a specified position in an array to another specified position in the array without changing the original array length.
array.copyWithin(target, start, end)
Copy the code
Copies the first two elements of the array onto the next two.
Fill – fills the function
Replaces the elements of an array with a fixed value.
array.fill(value, start, end)
Copy the code
Fill the array with fixed values
Batch replace the specified location
Just like it before you go! 🤞 🤞 🤞