Array Methods Flashcards
Joins arrays and returns an array with the joined arrays
concat()
array1.concat(array2, array3, …, arrayX)
The concat() method concatenates (joins) two or more arrays.
The concat() method returns a new array, containing the joined arrays.
The concat() method does not change the existing arrays.
Returns the function that created the Array object’s prototype
constructor
array.constructor
The constructor property returns the function that created the Array prototype.
For JavaScript arrays the constructor property returns:
function Array() { [native code] }
Copies array elements within the array, to and from specified positions
copyWithin()
array.copyWithin(target, start, end)
The copyWithin() method copies array elements to another position in the array.
The copyWithin() method overwrites the existing values.
The copyWithin() method does not add items to the array.
Returns a key/value pair Array Iteration Object
entries()
array.entries()
The entries() method does not change the original array.
Checks if every element in an array pass a test
every()
array.every(function(currentValue, index, arr), thisValue)
The every() method executes a function for each array element.
The every() method returns true if the function returns true for all elements.
The every() method returns false if the function returns false for one element.
The every() method does not execute the function for empty elements.
The every() method does not change the original array
Fill the elements in an array with a static value
fill()
array.fill(value, start, end)
The fill() method fills specified elements in an array with a value.
The fill() method overwrites the original array.
Start and end position can be specified. If not, all elements will be filled.
Creates a new array with every element in an array that passes a test
filter()
array.filter(function(currentValue, index, arr), thisValue)
The filter() method creates a new array filled with elements that pass a test provided by a function.
The filter() method does not execute the function for empty elements.
The filter() method does not change the original array.
Returns the value of the first element in an array that passes a test
find()
array.find(function(currentValue, index, arr),thisValue)
The find() method returns the value of the first element that passes a test.
The find() method executes a function for each array element.
The find() method returns undefined if no elements are found.
The find() method does not execute the function for empty elements.
The find() method does not change the original array.
Returns the index of the first element in an array that pass a test
findIndex()
array.findIndex(function(currentValue, index, arr), thisValue)
The findIndex() method executes a function for each array element.
The findIndex() method returns the index (position) of the first element that passes a test.
The findIndex() method returns -1 if no match is found.
The findIndex() method does not execute the function for empty array elements.
The findIndex() method does not change the original array.
Calls a function for each array element
forEach()
array.forEach(function(currentValue, index, arr), thisValue)
The forEach() method calls a function for each element in an array.
The forEach() method is not executed for empty elements.
Creates an array from an object
from()
Array.from(object, mapFunction, thisValue)
The Array.from() method returns an array from any object with a length property.
The Array.from() method returns an array from any iterable object.
Check if an array contains the specified element
includes()
array.includes(element, start)
The includes() method returns true if an array contains a specified value.
The includes() method returns false if the value is not found.
The includes() method is case sensitive.
Searches the array for an element and returns its position
indexOf()
array.indexOf(item, start)
The indexOf() method returns the first index (position) of a specified value.
The indexOf() method returns -1 if the value is not found.
The indexOf() method starts at a specified index and searches from left to right.
By default the search starts at the first element and ends at the last.
Negative start values counts from the last element (but still searches from left to right).
Checks whether an object is an array
isArray()
Array.isArray(obj)
The isArray() method returns true if an object is an array, otherwise false.
Joins all elements of an array into a string
join()
array.join(separator)
The join() method returns an array as a string.
The join() method does not change the original array.
Any separator can be specified. The default is comma (,).
Returns a Array Iteration Object, containing the keys of the original array
keys()
array.keys()
The keys() method returns an Array Iterator object with the keys of an array.
The keys() method does not change the original array.
Search the array for an element, starting at the end, and returns its position
lastIndexOf()
array.lastIndexOf(item, start)
The lastIndexOf() method returns the last index (position) of a specified value.
The lastIndexOf() method returns -1 if the value is not found.
The lastIndexOf() starts at a specified index and searches from right to left.
By defalt the search starts at the last element and ends at the first.
Negative start values counts from the last element (but still searches from right to left).
Sets or returns the number of elements in an array
length
array. length
array. length = number
The length property sets or returns the number of elements in an array.
Creates a new array with the result of calling a function for each array element
map()
array.map(function(currentValue, index, arr), thisValue)
map() creates a new array from calling a function for every array element.
map() calls a function once for each element in an array.
map() does not execute the function for empty elements.
map() does not change the original array.
Removes the last element of an array, and returns that element
pop()
array.pop()
The pop() method removes (pops) the last element of an array.
The pop() method changes the original array.
The pop() method returns the removed element.
Allows you to add properties and methods to an Array object
prototype
Array.prototype.name = value
prototype allows you to add new properties and methods to arrays.
prototype is a property available with all JavaScript objects.
Adds new elements to the end of an array, and returns the new length
push()
array.push(item1, item2, …, itemX)
The push() method adds new items to the end of an array.
The push() method changes the length of the array.
The push() method returns the new length.
Reduce the values of an array to a single value (going left-to-right)
reduce()
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
The reduce() method executes a reducer function for array element.
The reduce() method returns a single value: the function’s accumulated result.
The reduce() method does not execute the function for empty array elements.
The reduce() method does not change the original array.
Reduce the values of an array to a single value (going right-to-left)
reduceRight()
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
The reduceRight() method executes a reducer function for each array element.
The reduceRight() method works from right to left.
The reduceRight() method returns a single value: the function’s accumulated result.
The reduceRight() method does not execute the function for empty elements.
Reverses the order of the elements in an array
reverse()
array.reverse()
The reverse() method reverses the order of the elements in an array.
The reverse() method overwrites the original array.
Removes the first element of an array, and returns that element
shift()
array.shift()
The shift() method removes the first item of an array.
The shift() method changes the original array.
The shift() method returns the shifted element.
Selects a part of an array, and returns the new array
slice()
array.slice(start, end)
The slice() method returns selected elements in an array, as a new array.
The slice() method selects from a given start, up to a (not inclusive) given end.
The slice() method does not change the original array.
Checks if any of the elements in an array pass a test
some()
array.some(function(value, index, arr), this)
The some() method checks if any array elements pass a test (provided as a callback function).
The some() method executes the callback function once for each array element.
The some() method returns true (and stops) if the function returns true for one of the array elements.
The some() method returns false if the function returns false for all of the array elements.
The some() method does not execute the function for empty array elements.
The some() method does not change the original array.
Sorts the elements of an array
sort()
array.sort(compareFunction)
The sort() sorts the elements of an array.
The sort() overwrites the original array.
The sort() sorts the elements as strings in alphabetical and ascending order.
Adds/Removes elements from an array
splice()
array.splice(index, howmany, item1, ….., itemX)
The splice() method adds and/or removes array elements.
The splice() method overwrites the original array.
Converts an array to a string, and returns the result
toString()
array.toString()
The toString() method returns a string with array values separated by commas.
The toString() method does not change the original array.
Adds new elements to the beginning of an array, and returns the new length
unshift()
array.unshift(item1, item2, …, itemX)
The unshift() method adds new elements to the beginning of an array.
The unshift() method overwrites the original array.
Returns the primitive value of an array
valueOf()
array.valueOf()
The valueOf() method returns the array itself.
The valueOf() method does not change the original array.
fruits.valueOf() returns the same as fruits.