Array.Reduce() Flashcards
const array1 = [1, 2, 3, 4]; const reducer =
console.log(array1.reduce(reducer)); // 1 + 2 + 3 + 4
(accumulator, currentValue) => accumulator + currentValue;
const array1 = [1, 2, 3, 4]; const reducer =
console.log(array1.reduce(reducer, 5));
const reducer = (accumulator, currentValue) => accumulator + currentValue;
The reducer function takes four arguments:
1.
2.
3.
4.
Accumulator (acc) Current Value (cur) Current Index (idx) Source Array (src)
arr.reduce(_____(_____, _______[, index[, array]]), [, initialValue])
callback Function
accumulator
Current Value
The __________ adds the callback’s return values. It is the added value previously returned in the last invocation of the callback, or initialValue, if supplied (see below)
accumulator
The current element being processed in the array.
currentValue
the 3 optional values are:
currentIndex
array
initialValue
The current element being processed in the array.
currentValue
The index of the current element being processed in the array. Starts from index 0 if an initialValue is provided. Otherwise, starts from index 1.
currentIndex
A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initialValue will throw a TypeError.
initialValue
The first time the callback is called, _________ and __________ can be one of two values.
accumulator
currentValue
If initialValue is not provided, reduce() will execute the callback function starting at __________ skipping the first index. If initialValue is provided, it will start at _______
index 1
index 0
var sum = [0, 1, 2, 3].reduce( // code here ) // sum is 6
function (accumulator, currentValue) { return accumulator + currentValue; }
var total = [ 0, 1, 2, 3 ].reduce( // code here );
( accumulator, currentValue ) => accumulator + currentValue
To sum up the values contained in an array of objects, you must supply an_________, so that each item passes through your function.
initialValue
var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) { return \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ },0)
console.log(sum) // logs 6
accumulator + currentValue.x;
var initialValue = 0; var sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (accumulator, currentValue) { return accumulator + currentValue.x; },initialValue)
WRITE AS A ARROW FUNCTION
( (accumulator, currentValue) => accumulator + currentValue.x
,initialValue
);
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(accumulator, currentValue =>
); // flattened is [0, 1, 2, 3, 4, 5]
accumulator.concat(currentValue);
If you are using an environment compatible with Set and Array.from(), you could use
let orderedArray = // ->?
to get an array where duplicate items have been removed.
Array.from(new Set(myArray));
const array1 = [[0, 1], [2, 3], [4, 5]].____________(
(accumulator, currentValue) => accumulator.concat(currentValue)
);
console.log(array1); // expected output: Array [4, 5, 2, 3, 0, 1]
reduceRight
arr.reduce(callback(accumulator, currentValue[, _______[,______]]), [,________])
index
array
initial value
you use intiliaze a object with reduce by adding
comma with braces to the end of the function
what’s is this missing
var flattened = [[0, 1], [2, 3], [4, 5]].reduce( function(accumulator, currentValue) { return accumulator.concat(currentValue); },
);
array brackets at end
Calling reduce() on an empty array without an initialValue will __________
throw a TypeError.
[ ].reduce( maxCallback ); //
TypeError
_______ will increment the value of i, and then return the incremented value.
++i
________ will increment the value of i, but return the original value that i held before being incremented.
i++
i = 1;
j = ++i;
(i is ___, j is _____)
2
2
i = 1;
j = i++;
(i is ____, j is _____)
2
1
const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(array1.reduce(reducer)); // expected output:
10
const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4 console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_); // To get expected output of 10
array1.reduce(reducer)
Sum all the values of an array using arrow
var sum = [0, 1, 2, 3]
// sum is 6
reduce(
( accumulator, currentValue ) => accumulator + currentValue,
0 );
Sum all the values of an array using function
var sum = [0, 1, 2, 3]
// sum is 6
.reduce(function (accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
CODE HERE
console.log(array1); // expected output: Array [5, 4, 2, 3, 0, 1]
(accumulator, currentValue) => accumulator.concat(currentValue)
);
The _____________ method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
reduceRight()
The __________ method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
reducer
let arr = [1, 2, 3, 4, 5];
add all numbers in array
let result =
alert(result); // 15
arr.reduce((sum, current) => sum + current, 0);
let value = arr.reduce(function(\_\_\_,\_\_\_,\_\_\_\_\_,\_\_\_\_\_\_) { // ... }, initial);
previousValue, item, index, array
WHAT CAN BE REMOVED AND WHY
let arr = [1, 2, 3, 4, 5];
let result = arr.reduce((sum, current) => sum + current, 0);
alert(result); // 15
0
current value at 0
const numbers = [5, 10, 15]; const total = numbers.reduce( (accumulator, currentValue) => accumulator + currentValue, 44);
console.log(total);
74
30 from function + the 44 start point
var a = ['1', '2', '3', '4', '5']; var left = a.reduce
console.log(left); // “12345”
(function(prev, cur) { return prev + cur; });
var a = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’];
var right = a.reduceRight
console.log(right); // “54321”
(function(prev, cur) { return prev + cur; });
the _____ doesn’t have to be used to accumulate a number rather it can be used to return the higest price.
accumulator
const products = [ { name: 'hard drive', price: 59.99 }, { name: 'lighbulbs', price: 2.59 }, { name: 'paper towels', price: 6.99 }, { name: 'flatscreen monitor', price: 159.99 }, { name: 'cable ties', price: 19.99 }, { name: 'ballpoint pens', price: 4.49 } ];
const product = products .filter(product => product.price < 10) .reduce((highest, product) => { if (highest.price > product.price) { return highest; } return product; }, { price: 0 });