JS Array Flashcards
JavaScript Array concat() Method?
The concat() method is used to join two or more arrays.
This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.
JavaScript Array copyWithin() Method?
The copyWithin() method copies array elements to another position in the array, overwriting the existing values.
This method will never add more items to the array.
ex:
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”, “Kiwi”, “Papaya”];
fruits.copyWithin(2, 0, 2);
Result:
Banana,Orange,Banana,Orange,Kiwi,Papaya
copies the first to at the index of zero AFTER the copies and keeps the last two in tack
array.copyWithin(target, start, end)
JavaScript Array entries() Method?
The entries() method returns an Array Iterator object with key/value pairs.
For each item in the original array, the new iteration object will contain an array with the index as the key, and the item value as the value:
[0, “Banana”]
[1, “Orange”]
[2, “Apple”]
[3, “Mango”]
ex: var fruits = ["Banana", "Orange", "Apple", "Mango"]; var f = fruits.entries();
for (x of f) {
document.getElementById(“demo”).innerHTML += x;
}
Result: 0,Banana 1,Orange 2,Apple 3,Mango
JavaScript Array every() Method?
The every() method checks if all elements in an array pass a test (provided as a function).
The every() method executes the function once for each element present in the array:
If it finds an array element where the function returns a false value, every() returns false (and does not check the remaining values)
If no false occur, every() returns true
Note: every() does not execute the function for array elements without values.
Note: every() does not change the original array. Returns TRUE OR FALSE
JavaScript Array fill() Method?
The fill() method fills the specified elements in an array with a static value. You can specify the position of where to start and end the filling. If not specified, all elements will be filled.
array.fill(value, start, end) Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.fill("Kiwi", 2, 4); Resutlt: ["Banana", "Orange", "Kiwi", "Kiwi"];
JavaScript Array filter() Method?
The filter() method creates an array filled with all array elements that pass a test (provided as a function).
Note: filter() does not execute the function for array elements without values.
Note: filter() does not change the original array.
JavaScript Array find() Method?
The find() method returns the value of the first element in an array that pass a test (provided as a function).
The find() method executes the function once for each element present in the array:
If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
Otherwise it returns undefined
Note: find() does not execute the function for empty arrays.
Note: find() does not change the original array.
JavaScript Array findIndex() Method?
The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).
The findIndex() method executes the function once for each element present in the array:
If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)
Otherwise it returns -1
Note: findIndex() does not execute the function for array elements without values.
Note: findIndex() does not change the original array.
JavaScript Array forEach() Method?
array.forEach(function(currentValue, index, arr), thisValue)
currentValue Required. The value of the current element
index: Optional. The array index of the current element
arr Optional. The array object the current element belongs to
JavaScript Array includes() Method
The includes() method determines whether an array contains a specified element.
This method returns true if the array contains the element, and false if not.
JavaScript Array indexOf() Method?
The indexOf() method searches the array for the specified item, and returns its position.
The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.
Returns -1 if the item is not found.
If the item is present more than once, the indexOf method returns the position of the first occurence.
JavaScript Array isArray() Method?
The isArray() method determines whether an object is an array.
This function returns true if the object is an array, and false if not.
JavaScript Array join() Method?
The join() method returns the array as a string.
The elements will be separated by a specified separator. The default separator is comma (,).
Note: this method will not change the original array
JavaScript Array length Property?
The length property sets or returns the number of elements in an array.
Example:
array.length
JavaScript Array lastIndexOf() Method?
Definition and Usage The lastIndexOf() method searches the array for the specified item, and returns its position.
The search will start at the specified position, or at the end if no start position is specified, and end the search at the beginning of the array.
Returns -1 if the item is not found.
If the item to search for is present more than once, the lastIndexOf method returns the position of the last occurence.
Syntax
array.lastIndexOf(item, start)