Classify array methods by method properties
A method that changes the array itself
Three common methods:
splice(startIndex, deleteCount, ... items)
— Adds an element from the specified location, returns the deleted element (returns an empty array when added).sort(func(a,b))
— Array sort,Sort by character encoding order. The time complexity depends on how the browser is implemented.reverse()
Reverse array, equivalent in Google /Microsoft Edgesort((a,b)=>-1)
, the equivalent in Firefoxsort((a,b)=>1)
.
Add and delete (queue, stack implementation) method:
push(... items)
Add elements from the tail. Returns the array length after push.pop()
— Remove the tail element. Returns a tail element (not an array).unshift(... items)
Add elements from the header. Returns the array length after unshift.shift()
— Remove the header element. Returns the header element (not an array).
Treatment methods:
fill(value,start,end)
— Fill the start index with value to end, excluding end.copyWithin(targetIndex,start,end)
— Copy and replace elements from start to end(optional) after targetIndex. The length of the array remains the same.
Methods that do not affect the original array
Adding and deleting methods:
slice(start,end)
— shallow copy from ‘start’ index to ‘end’ index and return.concat(... items)
Merge the parameter elements into the array. If the argument is an array, the element is taken.
Query method:
indexOf(item)
— Query the specified element index without returning -1.lastIndexOf(item,start)
— Query the last position of the specified element. ‘start’ specifies the starting position of the query, optional.includes(item)
Return true if the array contains the element, false otherwise.find(func(item, index, arr))
— Returns one that satisfies the func() conditionThe first element.filter(func(item, index, arr))
— Returns the func() conditionAll elements (array).findeIndex(func(item, index, arr))
— Returns one that satisfies the func() conditionThe first element index value.
Treatment methods:
map(func(item, index, arr))
Returns a new array of elements returned by func.split(str,len)
Method of String that returns a split array of STR. STR: delimiter/regular expression, len returns the specified length of the array.join(str)
Insert STR between array elements to return the concatenated string. When no arguments are passed, elements are concatenated with commas.reduce(func(acc,item,index,arr),initialValue)
Returns the final iteration of the func processed array. Acc: result of last iteration; Item: current value (Let’s start with the second element); Index: index of the current executing element (Starting from 1), arR: original array, initialValue: initial ACC value of the first call to funC.reduceRight(func(acc,item,index,arr),initialValue)
— Equivalent to reduce(),func executes from the right.forEach(func(item, index, arr))
— Traverses the number group, no return value.isArray(arr)
— Checks whether arR is an array and returns a Boolean value.
Other:
from(arrlike,func(item),this)
Generates a new shallow-copy array from the class array object.of(... items)
Generates an array from the parameters passed in.entries()
Returns a new array iterator containing the key-value pairs for each index.keys()
– return byThe index keyConstitute an iterable.values()
– return byThe value of each indexConstitute an iterable.every(func(item,index,arr),this)
Whether or not –All elements satisfyFunc condition that returns a Boolean value true.some(func(item,index,arr),this)
—As long as there are elements that satisfyFunc condition that returns a Boolean value true.flat(depth)
Depth reduces the array to the specified depth, and returns a new array.flatMap()
— Equivalent to the flat() function at depth 1 after the map traverses the array.toString()
Returns a string of elements concatenated by commas.toLocaleString(locales,options)
Returns a string of elements concatenated by commas. (Locale can be specified)