JS Arrays Flashcards
array1.concat(array2, array3, …, arrayX)
Returns Array. Joins two or more arrays, and returns a copy of the joined arrays
array.copyWithin(target, ?start, ?end)
target: The index position to copy the elements to
start: The index position to start copying elements from (default is 0)
end: The index position to stop copying elements from (default is array.length)
Returns modified Array. Copies array elements within the array, to and from specified positions
array.every(function(currentValue, ?index, ?arr), ?thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns Boolean. Checks if every element in an array pass a test
array.fill(value, start, end)
Returns modified Array. Fill the elements in an array with a static value
array.filter(function(currentValue, index, arr), thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns new Array. Creates a new array with every element in an array that pass a test
array.find(function(currentValue, index, arr),thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns the value of the first element in an array that pass a test; returns undefined is none pass
array.findIndex(function(currentValue, index, arr), thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns the index of the first element in an array that pass a test; if none, returns -1
array.forEach(function(currentValue, index, arr), thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Calls a function for each array element
array.indexOf(item, ?start)
if start negative will count from end and continue to end
Search the array for an element and returns its position
Array.isArray(obj)
Return Boolean. Checks whether an object is an array
array.join(?separator)
if no separator, defaults to comma
Returns String. Joins all elements of an array into a string
array. lastIndexOf(item, ?start)
start: Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning
Search the array for an element, starting at the end, and returns its position
array.map(function(currentValue, index, arr), thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns new Array. Creates a new array with the result of calling a function for each array element
array.pop()
Removes the last element of an array, and returns that element, changes original array
array.push(item1, item2, …, itemX)
Adds new elements to the end of an array, and returns the new length
array.reduce(function(total, currentValue, currentIndex, arr), ?initialValue)
Function arguments:
total: The initialValue, or the previously returned value of the function
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
initialValue: A value to be passed to the function as the initial value
Reduce the values of an array to a single value (going left-to-right), Returns the accumulated result from the last call of the callback function
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
Function arguments:
total: The initialValue, or the previously returned value of the function
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
initialValue: A value to be passed to the function as the initial value
Reduce the values of an array to a single value (going right-to-left), Returns the accumulated result from the last call of the callback function
array.reverse()
Returns modified array. Reverses the order of the elements in an array
array.shift()
Removes the first element of an array, and returns that element, changes original array
array. slice(?start, ?end)
start: An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array. If omitted, it acts like “0”
end: An integer that specifies where to end the selection. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array
Selects a part of an array, and returns the new array
array.some(function(currentValue, index, arr), thisValue)
Function arguments:
currentValue: The value of the current element
index: The array index of the current element
arr: The array object the current element belongs to
thisValue: A value to be passed to the function to be used as its “this” value.
If this parameter is empty, the value “undefined” will be passed as its “this” value
Returns Boolean. Checks if any of the elements in an array pass a test
array.sort(?compareFunction)
Default sorts as strings alphabetically
compareFunction: A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like: function(a, b){return a-b} When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.
Ex. var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b});
Returns modified array. Sorts the elements of an array
array.splice(index, ?howmany, ?item1, ….., itemX)
index: An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array
howmany: The number of items to be removed. If set to 0, no items will be removed
item1, …, itemX: The new item(s) to be added to the array
Ex. var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 1, "Lemon", "Kiwi"); The result of fruits will be: Banana,Orange,Lemon,Kiwi,Mango
Returns a new Array, containing the removed items (if any). Modifies original array. Adds/Removes elements from an array
array.toString()
Converts an array to a string, and returns the result
array.unshift(item1, item2, …, itemX)
Adds new elements to the beginning of an array, and returns the new length
array.valueOf()
Returns the primitive value of an array