Array Methods Flashcards
(25 cards)
What parameters can be passed to the “map” method’s callback function?
item, index, array
What is the format of the “map” method and what does it do?
Use multiplyByTwo as an example.
The “map” method creates a new array populated with the results of calling a provided function on every element in the calling array.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const oldArr = [1, 2, 3];
function multiplyByTwo (item, index, array) { return item * 2; }
newArr = oldArr.map(multiplyByTwo);
List 4 array methods that return a new array
map, filter, reduce, slice
What is the format of the “filter” method and what does it do?
Use idOddNumber as an example.
The “filter” method creates a new array with all elements that pass the test implemented by the provided function.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const oldArr = [1, 2, 3];
function isOddNumber(item, index, array) { return item % 2 !== 0; };
const newArr = oldArr.filter(isOddNumber);
What is the format of the “find” method and what does it do?
Use idEqualToTen as an example.
The “find” method returns the value of the first element in the provided array that satisfies the provided testing function.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
function isEqualToTen(item, index, array) { return item === 10; };
const ten = oldArr.find(isEqualToTen);
What parameters can be passed to the “filter” method’s callback function?
item, index, array
What parameters can be passed to the “find” method’s callback function?
item, index, array
What parameters can be passed to the “forEach” method’s callback function?
item, index, array
What is the format of the “forEach” method and what does it do?
Use idEqualToTen as an example.
The “forEach” method executes a provided function once for each array element
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const oldArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; const newArr = [];
function isEqualToTen(item, index, array) { if(item === 10) { newArr.push(item); } };
oldArr.forEach(isEqualToTen);
What is the difference between “map” and “forEach”?
map returns a new array and forEach does not
What parameters can be passed to the “every” method’s callback function?
item, index, array
What is the format of the “every” method and what does it do?
Use isGreaterThanTen as an example.
The “every” method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const myArr = [12, 13, 14, 27, 80];
function isGreaterThanTen(item, index, array) { if (item > 10) { return true; } }
myArr.every(isGreaterThanTen);
What parameters can be passed to the “some” method’s callback function?
item, index, array
What is the format of the “some” method and what does it do?
Use isGreaterThanTen as an example.
The “some” method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const myArr = [1, 3, 12, 13, 14, 27, 80];
function isGreaterThanTen(item, index, array) { if (item > 10) { return true; } }
myArr.some(isGreaterThanTen);
What is the format of the “reverse” method and what does it do?
The “reverse” method reverses and array
_________________________
const myArr = [1, 3, 12, 13, 14, 27, 80];
myArr.reverse();
What is the format of the “includes” method and what does it do?
The “includes” method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
_______________________
const myArr = [1, 3, 12, 13, 14, 27, 80];
myArr.includes(2);
What parameters can be passed to the “includes” method’s callback function?
valueToFind, fromIndex
List at least 8 array methods that alter an existing array
forEach, reverse, sort, splice, pop, push, shift, unshift
What is the format of the “shift” method and what does it do?
The “shift” method removes the first element from an array and returns that removed element. This method changes the length of the array.
_______________________
What is the format of the “unshift” method and what does it do?
The “unshift” method adds one or more elements to the beginning of an array and returns the new length of the array.
_______________________
What is the difference between “shift” and “unshift”?
Shift removes an item from the array. Unshift adds an item to the array.
What is the format of the “splice” method and what does it do?
The “splice” method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ const oldArr = ["maia", "matteo", "emilia", "lucia"]; const newArr = oldArr.splice("frankie",1,3);
//expect newArr = [“matteo”, “emilia”]
What is the format of the “slice” method and what does it do?
The “slice” method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
______________
const oldArr = ["maia", "matteo", "emilia", "lucia"]; const newArr = oldArr.slice(1,3);
//expect newArr = [“matteo”, “emilia”]
What is the different between “map” and “filter”?
Map will always return an array of the same length whereas filter will return an array that contains a subset of the original array.