JavaScript provides a wide variety of array methods that make working with arrays more convenient and efficient. Here's an overview of the most commonly used JavaScript array methods:
1. Mutator Methods
These methods modify the array they are called on.
push(element)
: Adds one or more elements to the end of an array and returns the new length.pop()
: Removes the last element from an array and returns that element.shift()
: Removes the first element from an array and returns that element.unshift(element)
: Adds one or more elements to the beginning of an array and returns the new length.splice(start, deleteCount, item1, item2, ...)
: Adds, removes, or replaces elements in an array. Returns an array of the removed elements.reverse()
: Reverses the order of the elements in an array in place.sort(compareFunction)
: Sorts the elements of an array in place and returns the sorted array.fill(value, start, end)
: Fills all the elements from a start index to an end index with a static value.
2. Accessor Methods
These methods return a new array or value without modifying the original array.
concat(array1, array2, ...)
: Merges two or more arrays and returns a new array.slice(start, end)
: Returns a shallow copy of a portion of an array into a new array object.includes(element, start)
: Checks if an array contains a certain element, returningtrue
orfalse
.indexOf(element, start)
: Returns the first index at which a given element can be found, or-1
if it is not present.lastIndexOf(element, start)
: Returns the last index at which a given element can be found, or-1
if it is not present.join(separator)
: Joins all elements of an array into a string, separated by a specified separator.toString()
: Returns a string representing the array and its elements.flat(depth)
: Creates a new array with all sub-array elements concatenated into it recursively up to a specified depth.flatMap(callback)
: Maps each element using a mapping function, then flattens the result into a new array.
3. Iteration Methods
These methods perform a function on each array element.
forEach(callback(element, index, array))
: Executes a provided function once for each array element.map(callback(element, index, array))
: Creates a new array populated with the results of calling a provided function on every element.filter(callback(element, index, array))
: Creates a new array with all elements that pass the test implemented by the provided function.reduce(callback(accumulator, currentValue, index, array), initialValue)
: Executes a reducer function on each element of the array, resulting in a single output value.reduceRight(callback(accumulator, currentValue, index, array), initialValue)
: Same asreduce()
, but works from right to left.some(callback(element, index, array))
: Checks if at least one element in the array passes the test implemented by the provided function. Returns a Boolean.every(callback(element, index, array))
: Checks if all elements in the array pass the test implemented by the provided function. Returns a Boolean.find(callback(element, index, array))
: Returns the value of the first element that satisfies the provided testing function. Otherwise, it returnsundefined
.findIndex(callback(element, index, array))
: Returns the index of the first element that satisfies the provided testing function. Otherwise, it returns-1
.
4. Other Useful Methods
Array.isArray(value)
: Checks if the passed value is an array. Returnstrue
orfalse
.from(arrayLike, mapFn)
: Creates a new, shallow-copiedArray
instance from an array-like or iterable object.of(element0, element1, ...)
: Creates a newArray
instance with a variable number of arguments.
No comments:
Post a Comment