Array Methods Flashcards
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();