Created
May 15, 2020 13:41
-
-
Save sangkukbae12/78884c4ca687015f6fb8e381111b20d4 to your computer and use it in GitHub Desktop.
Reduce Data with Javascript Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Transform an Array into a Single Value using Reduce | |
var data = [15, 3, 20]; | |
var reducer = function(accumulator, item) { | |
return accumulator + item; | |
}; | |
var initialValue = 0; | |
var total = data.reduce(reducer, initialValue); | |
console.log("The sum is: ", total); | |
// 2. Reduce an Array into a Single Object | |
//see CONSOLE! | |
var votes = [ | |
"angular", | |
"angular", | |
"react", | |
"react", | |
"react", | |
"angular", | |
"ember", | |
"react", | |
"vanilla" | |
]; | |
var initialValue = {}; | |
var reducer = function(tally, vote) { | |
if (!tally[vote]) { | |
tally[vote] = 1; | |
} else { | |
tally[vote] = tally[vote] + 1; | |
} | |
return tally; | |
}; | |
var result = votes.reduce(reducer, initialValue); | |
// Output | |
console.log("Angular: ", result.angular) | |
console.log("React: ", result.react) | |
console.log("Ember: ", result.ember) | |
console.log("Vanilla: ", result.vanilla) | |
// 3. Use Reduce to Filter and Map over Large Datasets | |
var data = [1, 2, 3]; | |
var doubled = data.reduce(function(acc, value) { | |
acc.push(value * 2); | |
return acc; | |
}, []); | |
var doubleMapped = data.map(function(item) { | |
return item * 2; | |
}); | |
var data2 = [1, 2, 3, 4, 5, 6]; | |
var evens = data2.reduce(function(acc, value) { | |
if (value % 2 === 0) { | |
acc.push(value); | |
} | |
return acc; | |
}, []); | |
var evenFiltered = data2.filter(function(item) { | |
return (item % 2 === 0); | |
}); | |
var filterMapped = data2.filter(function(value) { | |
return value % 2 === 0; | |
}).map(function(value) { | |
return value * 2; | |
}); | |
var bigData = []; | |
for (var i = 0; i < 1000000; i++) { | |
bigData[i] = i; | |
} | |
let filterBegin = Date.now() | |
var filterMappedBigData = bigData.filter(function(value) { | |
return value % 2 === 0; | |
}).map(function(value) { | |
return value * 2; | |
}); | |
let filterEnd = Date.now() | |
let filtertimeSpent = (filterEnd-filterBegin)/1000 + "secs"; | |
let reducedBegin=Date.now(); | |
var reducedBigData = bigData.reduce(function(acc, value) { | |
if (value % 2 === 0) { | |
acc.push(value * 2); | |
} | |
return acc; | |
}, []); | |
let reducedEnd = Date.now(); | |
let reducedtimeSpent = (reducedEnd-reducedBegin)/1000 + " secs"; | |
console.log("filtered Big Data:", filtertimeSpent) | |
console.log("reduced Big Data:", reducedtimeSpent) | |
// 4. Use the Optional Reduce Arguments | |
function reducer(accumulator, value, index, array) { | |
var intermediaryValue = accumulator + value; | |
if (index === array.length - 1) { | |
return intermediaryValue / array.length; | |
} | |
return intermediaryValue; | |
} | |
var data = [1, 2, 3, 3, 4, 5, 3, 1]; | |
var mean = data.reduce(reducer, 0); | |
console.log("Mean:", mean) | |
// 5. Avoid Common Mistakes when Working with Reduce | |
var data = ["vote1", "vote2", "vote1", "vote2"]; | |
function reducer(accumulator, value) { | |
if (accumulator[value]) { | |
accumulator[value] = accumulator[value] + 1; | |
} else { | |
accumulator[value] = 1; | |
} | |
return accumulator; | |
} | |
var tally = data.reduce(reducer, {}); | |
console.log("Vote1: ", tally.vote1) | |
// 6. Learn to Flatten and Flatmap Arrays with Reduce | |
var data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | |
var flattenedData = data.reduce(function(acc, value) { | |
return acc.concat(value); | |
}, []); | |
var input = [ | |
{ | |
title: "Batman Begins", | |
year: 2005, | |
cast: [ | |
"Christian Bale", | |
"Michael Caine", | |
"Liam Neeson", | |
"Katie Holmes", | |
"Gary Oldman", | |
"Cillian Murphy" | |
] | |
}, | |
{ | |
title: "The Dark Knight", | |
year: 2008, | |
cast: [ | |
"Christian Bale", | |
"Heath Ledger", | |
"Aaron Eckhart", | |
"Michael Caine", | |
"Maggie Gyllenhal", | |
"Gary Oldman", | |
"Morgan Freeman" | |
] | |
}, | |
{ | |
title: "The Dark Knight Rises", | |
year: 2012, | |
cast: [ | |
"Christian Bale", | |
"Gary Oldman", | |
"Tom Hardy", | |
"Joseph Gordon-Levitt", | |
"Anne Hathaway", | |
"Marion Cotillard", | |
"Morgan Freeman", | |
"Michael Caine" | |
] | |
} | |
]; | |
var stars = input.reduce(function(acc, value) { | |
value.cast.forEach(function(star) { | |
if (acc.indexOf(star) === -1) { | |
acc.push(star); | |
} | |
}); | |
return acc; | |
}, []); | |
var data = [1, 2, 3, 4, "5"]; | |
var sum = data.reduceRight(function(acc, value, index) { | |
console.log(`Index: ${index}`) | |
return acc + value; | |
}, 0); | |
console.log("Sum: ", sum) | |
// 7. Compose Functions with Reduce | |
function increment(input) { return input + 1;} | |
function decrement(input) { return input - 1; } | |
function double(input) { return input * 2; } | |
function halve(input) { return input / 2; } | |
var initial_value = 1; | |
var pipeline = [ | |
increment, | |
increment, | |
increment, | |
double, | |
increment, | |
increment, | |
halve | |
]; | |
var final_value = pipeline.reduce(function(acc, fn) { | |
return fn(acc); | |
}, initial_value); | |
var reversed = pipeline.reduceRight(function(acc, fn) { | |
return fn(acc); | |
}, initial_value) | |
console.log("final_value: ", final_value) | |
console.log("reversed: ", reversed) | |
// 8. Safely inspect nested objects with Reduce | |
var luke = { | |
name: "luke skywalker", | |
jedi: true, | |
parents: { | |
father: { | |
jedi: true | |
}, | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var han = { | |
name: "han solo", | |
jedi: false, | |
parents: { | |
father: { | |
jedi: false | |
}, | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var anakin = { | |
name: "anakin skywalker", | |
jedi: true, | |
parents: { | |
mother: { | |
jedi: false | |
} | |
} | |
} | |
var characters = [luke, han, anakin]; | |
function fatherWasJedi(character) { | |
var path = "parents.father.jedi"; | |
return path.split(".").reduce(function(obj, field) { | |
if (obj) { | |
return obj[field]; | |
} | |
return false; | |
}, character); | |
} | |
characters.forEach(function(character) { | |
console.log(character.name + "'s father was a jedi:", fatherWasJedi(character)) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment