Skip to content

Instantly share code, notes, and snippets.

@bhuizi
Created August 28, 2016 21:30
Show Gist options
  • Save bhuizi/bd5ba6dd69f523db18751e899fb73b9c to your computer and use it in GitHub Desktop.
Save bhuizi/bd5ba6dd69f523db18751e899fb73b9c to your computer and use it in GitHub Desktop.
reduce data w/ array reduce
//01 transform
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);
//02 array into obj
//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);
console.log(result);
//03 patterns
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;
}
// console.time('bigData');
var filterMappedBigData = bigData.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
});
// console.timeEnd('bigData');
// console.time('bigDataReduce');
var reducedBigData = bigData.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value * 2);
}
return acc;
}, []);
// console.timeEnd('bigDataReduce');
//04 additional args
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);
//06 flatten, flatmap, reduceright
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);
return acc + value;
}, 0);
console.log(sum);
//07 composing funcs w/ 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, reversed);
//08 nested obj inspect
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