Skip to content

Instantly share code, notes, and snippets.

@RickJP
Created April 10, 2019 09:45
Show Gist options
  • Save RickJP/b9ef38e967da13d33e57f6d07143fdb1 to your computer and use it in GitHub Desktop.
Save RickJP/b9ef38e967da13d33e57f6d07143fdb1 to your computer and use it in GitHub Desktop.
JS - REDUCE (Examples)
const euros = [ 29.76, 41.85, 46.5 ];
euros.forEach((val) => {
// console.log(val);
});
const total = euros.reduce((total, amount) => {
total += amount;
return total;
});
const average = euros.reduce((total, amount, index, array) => {
total += amount;
return total / array.length;
}, 0);
const double = euros.reduce((total, amount, index, array) => {
total.push(amount * 2);
return total;
}, []);
const above30 = euros.reduce((total, amount) => {
if (amount > 30) {
total.push(amount);
}
return total;
}, []);
// console.log(`Total is ${total}`);
// console.log(`Average is ${average}`);
// console.log(`Doubled values are ${double}`);
// console.log(`Values above 30 are ${above30}`);
const fruitBasket = [
'cherry',
'grapes',
'apple',
'blackcurrent',
'strawberry',
'mango',
'cherry',
'apple',
'grapes',
'mango',
'mango'
];
// Keep a tally of items
const fruitCount = fruitBasket.reduce((tally, fruit) => {
tally[fruit] = (tally[fruit] || 0) + 1;
return tally;
}, {});
// console.log(fruitCount);
// Flatten a series of array
const data1 = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
const flat = data1.reduce((total, amount) => {
return total.concat(amount);
}, []);
// console.log(flat);
const data2 = [
{ a: 'happy', b: 'robin', c: [ 'blue', 'green' ] },
{ a: 'tired', b: 'panther', c: [ 'green', 'black', 'orange', 'blue' ] },
{ a: 'sad', b: 'goldfish', c: [ 'green', 'red' ] }
];
// Reduce object array down to values
const colors = data2.reduce((total, amount) => {
amount.c.forEach((color) => {
total.push(color);
});
return total;
}, []);
// console.log(colors);
// Reduce object array down to unique values
const uniqueColors = data2.reduce((total, amount) => {
amount.c.forEach((color) => {
if (total.indexOf(color) === -1) {
total.push(color);
}
});
return total;
}, []);
// console.log(uniqueColors);
// Call functions using a pipeline
function incrementIt(input) {
return input + 1;
}
function decrementIt(input) {
return input - 1;
}
function doubleIt(input) {
return input * 2;
}
function halveIt(input) {
return input / 2;
}
let pipeline = [ incrementIt, doubleIt, decrementIt ];
const result = pipeline.reduce((total, func) => {
return func(total);
}, 1);
// console.log(`Result of pipeline processing: ${result}`);
const dollars = [ 32, 45, 50 ];
console.log(dollars);
const moreEuros = dollars.map((eachAmount) => eachAmount * 0.93);
console.log(`Converted Dollars To euros: ${moreEuros}`);
const euros1 = [ 34.2, 99.43, 11.0 ];
console.log(euros1);
const sum = euros1.reduce((total, amount) => total + amount, 0);
console.log(`Sum of Euros: ${sum}`);
const euros2 = [ 29.65, 56, 12, 46.5, 22.32 ];
const above_30 = euros2.filter((euro) => euro >= 30);
console.log(above_30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment