Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Last active October 22, 2021 20:21
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save elijahmanor/6222466 to your computer and use it in GitHub Desktop.
Save elijahmanor/6222466 to your computer and use it in GitHub Desktop.
Reducing Filter and Map with Reduce
var doctors = [
{ number: 1, actor: "William Hartnell", begin: 1963, end: 1966 },
{ number: 2, actor: "Patrick Troughton", begin: 1966, end: 1969 },
{ number: 3, actor: "Jon Pertwee", begin: 1970, end: 1974 },
{ number: 4, actor: "Tom Baker", begin: 1974, end: 1981 },
{ number: 5, actor: "Peter Davison", begin: 1982, end: 1984 },
{ number: 6, actor: "Colin Baker", begin: 1984, end: 1986 },
{ number: 7, actor: "Sylvester McCoy", begin: 1987, end: 1989 },
{ number: 8, actor: "Paul McGann", begin: 1996, end: 1996 },
{ number: 9, actor: "Christopher Eccleston", begin: 2005, end: 2005 },
{ number: 10, actor: "David Tennant", begin: 2005, end: 2010 },
{ number: 11, actor: "Matt Smith", begin: 2010, end: 2013 },
{ number: 12, actor: "Peter Capaldi", begin: 2013, end: 2013 }
];
doctors = doctors.filter(function(doctor) {
return doctor.begin > 2000; // if truthy then keep item
}).map(function(doctor) {
return { // return what new object will look like
doctorNumber: "#" + doctor.number,
playedBy: doctor.actor,
yearsPlayed: doctor.end - doctor.begin + 1
};
});
console.log(JSON.stringify(doctors, null, 4));
[
{ doctorNumber: "#9", playedBy: "Christopher Eccleston", yearsPlayed: 1 },
{ doctorNumber: "#10", playedBy: "David Tennant", yearsPlayed: 6 },
{ doctorNumber: "#11", playedBy: "Matt Smith", yearsPlayed: 4 },
{ doctorNumber: "#12", playedBy: "Peter Capaldi", yearsPlayed: 1 }
]
doctors = doctors.reduce(function(memo, doctor) {
if (doctor.begin > 2000) { // this serves as our `filter`
memo.push({ // this serves as our `map`
doctorNumber: "#" + doctor.number,
playedBy: doctor.actor,
yearsPlayed: doctor.end - doctor.begin + 1
});
}
return memo;
}, []);
console.log(JSON.stringify(doctors, null, 4));
var developers = [
{ name: "Joe", age: 23 },
{ name: "Sue", age: 28 },
{ name: "Jon", age: 32 },
{ name: "Bob", age: 24 }
], age = 0;
age = developers.reduce(function(memo, developer) {
return memo + developer.age; // return previous total plus current age
}, 0); // initialize age with 0 that will be passed as memo
console.log("Sum of all developer ages is " + age);
// Output: Sum of all developer ages is 107
doctors = _.chain(doctors) // starts chain using the doctors array
.filter(function(doctor) { // uses array from chain
return doctor.begin > 2000;
})
.map(function(doctor) { // uses array from chain
return {
doctorNumber: "#" + doctor.number,
playedBy: doctor.actor,
yearsPlayed: doctor.end - doctor.begin + 1
};
})
.value(); // gets value from chain
console.log(JSON.stringify(doctors, null, 4));
doctors = _.filter(doctors, function(doctor) {
return doctor.begin > 2000;
});
doctors = _.map(doctors, function(doctor) {
return {
doctorNumber: "#" + doctor.number,
playedBy: doctor.actor,
yearsPlayed: doctor.end - doctor.begin + 1
};
});
console.log(JSON.stringify(doctors, null, 4));
doctors = _.reduce(doctors, function(memo, doctor) {
if (doctor.begin > 2000) {
memo.push({
doctorNumber: "#" + doctor.number,
playedBy: doctor.actor,
yearsPlayed: doctor.end - doctor.begin + 1
});
}
return memo;
}, []);
console.log(JSON.stringify(doctors, null, 4));
@amit3vr
Copy link

amit3vr commented Apr 14, 2016

good idea!

@kangalert
Copy link

What is this _.chain and _reduce means? I run to run it I got an error.

@nachodd
Copy link

nachodd commented May 21, 2018

@kangalert are functions from underscore library: http://underscorejs.org/#chain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment