In this section, you will learn to utilize Lodash _.groupBy
to perform some simple
aggregate analysis.
Some of the Lodash functions you will use are:
First, study the working examples below carefully. Then, complete the exercises that follow.
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "1": [ { "name": "Peter Pan", "age": 15 } ], "3": [ { "name": "Kelly Fan", "age": 35 }, { "name": "Ben Smith", "age": 35 } ], "4": [ { "name": "Mary Smith", "age": 42 }, { "name": "Adam Potts", "age": 42 }, { "name": "Joe Johnson", "age": 46 } ], "5": [ { "name": "John Smith", "age": 54 } ] }
{ "1": [ { "name": "Peter Pan", "age": 15 } ], "3": [ { "name": "Kelly Fan", "age": 35 }, { "name": "Ben Smith", "age": 35 } ], "4": [ { "name": "Mary Smith", "age": 42 }, { "name": "Adam Potts", "age": 42 }, { "name": "Joe Johnson", "age": 46 } ], "5": [ { "name": "John Smith", "age": 54 } ] }
var result = _.groupBy(data, function(d){ return Math.floor(d.age / 10) }) return result
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
[ "1", "3", "4", "5" ]
[ "1", "3", "4", "5" ]
var groups = _.groupBy(data, function(d){ return Math.floor(d.age / 10) }) var result = _.keys(groups) return result
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "1": 1, "3": 2, "4": 3, "5": 1 }
{ "1": 1, "3": 2, "4": 3, "5": 1 }
var groups = _.groupBy(data, function(d){ return Math.floor(d.age / 10) }) var result = _.mapValues(groups, function(value){ return value.length }) return result
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "1": "Peter Pan", "3": "Kelly Fan", "4": "Mary Smith", "5": "John Smith" }
{ "1": "Peter Pan", "3": "Kelly Fan", "4": "Mary Smith", "5": "John Smith" }
var groups = _.groupBy(data, function(d){ return Math.floor(d.age / 10) }) var result = _.mapValues(groups, function(value){ return value[0].name }) return result
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "Smith": [ { "name": "John Smith", "age": 54 }, { "name": "Mary Smith", "age": 42 }, { "name": "Ben Smith", "age": 35 } ], "Pan": [ { "name": "Peter Pan", "age": 15 } ], "Fan": [ { "name": "Kelly Fan", "age": 35 } ], "Potts": [ { "name": "Adam Potts", "age": 42 } ], "Johnson": [ { "name": "Joe Johnson", "age": 46 } ] }
{ "Smith": [ { "name": "John Smith", "age": 54 }, { "name": "Mary Smith", "age": 42 }, { "name": "Ben Smith", "age": 35 } ], "Pan": [ { "name": "Peter Pan", "age": 15 } ], "Fan": [ { "name": "Kelly Fan", "age": 35 } ], "Potts": [ { "name": "Adam Potts", "age": 42 } ], "Johnson": [ { "name": "Joe Johnson", "age": 46 } ] }
var results = _.groupBy(data, function(d){ return d.name.split(' ')[1] }) return results
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "Smith": 3, "Pan": 1, "Fan": 1, "Potts": 1, "Johnson": 1 }
{ "Smith": 3, "Pan": 1, "Fan": 1, "Potts": 1, "Johnson": 1 }
var groups = _.groupBy(data, function(d){ return d.name.split(' ')[1] }) var result = _.mapValues(groups, function(value){ return value.length }) return result
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Kelly Fan', age: 35}, {name: 'Adam Potts', age: 42}, {name: 'Joe Johnson', age: 46}, {name: 'Ben Smith', age: 35}]
{ "Smith": "John Smith", "Pan": "Peter Pan", "Fan": "Kelly Fan", "Potts": "Adam Potts", "Johnson": "Joe Johnson" }
{ "Smith": "John Smith", "Pan": "Peter Pan", "Fan": "Kelly Fan", "Potts": "Adam Potts", "Johnson": "Joe Johnson" }
var groups = _.groupBy(data, function(d){ return d.name.split(' ')[1] }) var result = _.mapValues(groups, function(value){ return value[0].name }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'travel']}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening']}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming']}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food']}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music']}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming']}]
[ { "name": "John Smith", "favorite": "food" }, { "name": "John Smith", "favorite": "travel" }, { "name": "Mary Smith", "favorite": "sports" }, { "name": "Mary Smith", "favorite": "travel" }, { "name": "Peter Pan", "favorite": "movies" }, { "name": "Peter Pan", "favorite": "tv" }, { "name": "Peter Pan", "favorite": "gardening" }, { "name": "Kelly Fan", "favorite": "movies" }, { "name": "Kelly Fan", "favorite": "travel" }, { "name": "Kelly Fan", "favorite": "programming" }, { "name": "Adam Potts", "favorite": "sports" }, { "name": "Adam Potts", "favorite": "food" }, { "name": "Joe Johnson", "favorite": "tv" }, { "name": "Joe Johnson", "favorite": "sports" }, { "name": "Joe Johnson", "favorite": "music" }, { "name": "Ben Smith", "favorite": "movies" }, { "name": "Ben Smith", "favorite": "tv" }, { "name": "Ben Smith", "favorite": "programming" } ]
[ { "name": "John Smith", "favorite": "food" }, { "name": "John Smith", "favorite": "travel" }, { "name": "Mary Smith", "favorite": "sports" }, { "name": "Mary Smith", "favorite": "travel" }, { "name": "Peter Pan", "favorite": "movies" }, { "name": "Peter Pan", "favorite": "tv" }, { "name": "Peter Pan", "favorite": "gardening" }, { "name": "Kelly Fan", "favorite": "movies" }, { "name": "Kelly Fan", "favorite": "travel" }, { "name": "Kelly Fan", "favorite": "programming" }, { "name": "Adam Potts", "favorite": "sports" }, { "name": "Adam Potts", "favorite": "food" }, { "name": "Joe Johnson", "favorite": "tv" }, { "name": "Joe Johnson", "favorite": "sports" }, { "name": "Joe Johnson", "favorite": "music" }, { "name": "Ben Smith", "favorite": "movies" }, { "name": "Ben Smith", "favorite": "tv" }, { "name": "Ben Smith", "favorite": "programming" } ]
// hint: use nested _.map, then _.flatten var result = _.map(data, function(element){ var name=element.name return _.map(element.favorites, function(favorite){ return {"name": name, "favorite": favorite} }) }) return _.flatten(result)
[{name: 'John Smith', age: 54, favorites: ['food', 'travel']}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening']}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming']}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food']}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music']}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming']}]
[ { "age": 15, "favorite": "movies" }, { "age": 15, "favorite": "tv" }, { "age": 15, "favorite": "gardening" }, { "age": 35, "favorite": "movies" }, { "age": 35, "favorite": "travel" }, { "age": 35, "favorite": "programming" }, { "age": 35, "favorite": "movies" }, { "age": 35, "favorite": "tv" }, { "age": 35, "favorite": "programming" }, { "age": 42, "favorite": "sports" }, { "age": 42, "favorite": "travel" }, { "age": 42, "favorite": "sports" }, { "age": 42, "favorite": "food" }, { "age": 46, "favorite": "tv" }, { "age": 46, "favorite": "sports" }, { "age": 46, "favorite": "music" }, { "age": 54, "favorite": "food" }, { "age": 54, "favorite": "travel" } ]
[ { "age": 15, "favorite": "movies" }, { "age": 15, "favorite": "tv" }, { "age": 15, "favorite": "gardening" }, { "age": 35, "favorite": "movies" }, { "age": 35, "favorite": "travel" }, { "age": 35, "favorite": "programming" }, { "age": 35, "favorite": "movies" }, { "age": 35, "favorite": "tv" }, { "age": 35, "favorite": "programming" }, { "age": 42, "favorite": "sports" }, { "age": 42, "favorite": "travel" }, { "age": 42, "favorite": "sports" }, { "age": 42, "favorite": "food" }, { "age": 46, "favorite": "tv" }, { "age": 46, "favorite": "sports" }, { "age": 46, "favorite": "music" }, { "age": 54, "favorite": "food" }, { "age": 54, "favorite": "travel" } ]
// hint: use nested _.map, then _.flatten var list = _.map(data, function(element){ var age=element.age return _.map(element.favorites, function(favorite){ return {"age": age, "favorite": favorite} }) }) return _.sortBy(_.flatten(list), function(element){ return element.age })
[{name: 'John Smith', age: 54, favorites: ['food', 'travel']}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening']}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming']}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food']}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music']}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming']}]
{ "food": [ { "name": "John Smith", "favorite": "food" }, { "name": "Adam Potts", "favorite": "food" } ], "travel": [ { "name": "John Smith", "favorite": "travel" }, { "name": "Mary Smith", "favorite": "travel" }, { "name": "Kelly Fan", "favorite": "travel" } ], "sports": [ { "name": "Mary Smith", "favorite": "sports" }, { "name": "Adam Potts", "favorite": "sports" }, { "name": "Joe Johnson", "favorite": "sports" } ], "movies": [ { "name": "Peter Pan", "favorite": "movies" }, { "name": "Kelly Fan", "favorite": "movies" }, { "name": "Ben Smith", "favorite": "movies" } ], "tv": [ { "name": "Peter Pan", "favorite": "tv" }, { "name": "Joe Johnson", "favorite": "tv" }, { "name": "Ben Smith", "favorite": "tv" } ], "gardening": [ { "name": "Peter Pan", "favorite": "gardening" } ], "programming": [ { "name": "Kelly Fan", "favorite": "programming" }, { "name": "Ben Smith", "favorite": "programming" } ], "music": [ { "name": "Joe Johnson", "favorite": "music" } ] }
{ "food": [ { "name": "John Smith", "favorite": "food" }, { "name": "Adam Potts", "favorite": "food" } ], "travel": [ { "name": "John Smith", "favorite": "travel" }, { "name": "Mary Smith", "favorite": "travel" }, { "name": "Kelly Fan", "favorite": "travel" } ], "sports": [ { "name": "Mary Smith", "favorite": "sports" }, { "name": "Adam Potts", "favorite": "sports" }, { "name": "Joe Johnson", "favorite": "sports" } ], "movies": [ { "name": "Peter Pan", "favorite": "movies" }, { "name": "Kelly Fan", "favorite": "movies" }, { "name": "Ben Smith", "favorite": "movies" } ], "tv": [ { "name": "Peter Pan", "favorite": "tv" }, { "name": "Joe Johnson", "favorite": "tv" }, { "name": "Ben Smith", "favorite": "tv" } ], "gardening": [ { "name": "Peter Pan", "favorite": "gardening" } ], "programming": [ { "name": "Kelly Fan", "favorite": "programming" }, { "name": "Ben Smith", "favorite": "programming" } ], "music": [ { "name": "Joe Johnson", "favorite": "music" } ] }
// hint: first, apply _.groupBy to the name-favovrite pairs computed earlier var list = _.map(data, function(element){ var name=element.name return _.map(element.favorites, function(favorite){ return {"name": name, "favorite": favorite} }) }) var groups = _.groupBy(_.flatten(list), function(d){ return d.favorite }) return groups
[{name: 'John Smith', age: 54, favorites: ['food', 'travel']}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening']}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming']}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food']}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music']}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming']}]
{ "food": [ "John Smith", "Adam Potts" ], "travel": [ "John Smith", "Mary Smith", "Kelly Fan" ], "sports": [ "Mary Smith", "Adam Potts", "Joe Johnson" ], "movies": [ "Peter Pan", "Kelly Fan", "Ben Smith" ], "tv": [ "Peter Pan", "Joe Johnson", "Ben Smith" ], "gardening": [ "Peter Pan" ], "programming": [ "Kelly Fan", "Ben Smith" ], "music": [ "Joe Johnson" ] }
{ "food": [ "John Smith", "Adam Potts" ], "travel": [ "John Smith", "Mary Smith", "Kelly Fan" ], "sports": [ "Mary Smith", "Adam Potts", "Joe Johnson" ], "movies": [ "Peter Pan", "Kelly Fan", "Ben Smith" ], "tv": [ "Peter Pan", "Joe Johnson", "Ben Smith" ], "gardening": [ "Peter Pan" ], "programming": [ "Kelly Fan", "Ben Smith" ], "music": [ "Joe Johnson" ] }
var list = _.map(data, function(element){ var name=element.name return _.map(element.favorites, function(favorite){ return {"name": name, "favorite": favorite} }) }) var groups = _.groupBy(_.flatten(list), function(d){ return d.favorite }) var result = _.mapValues(groups, function(value){ return _.map(value, function(element){ return element.name }) }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'travel']}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening']}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming']}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food']}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music']}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming']}]
{ "food": 2, "travel": 3, "sports": 3, "movies": 3, "tv": 3, "gardening": 1, "programming": 2, "music": 1 }
{ "food": 2, "travel": 3, "sports": 3, "movies": 3, "tv": 3, "gardening": 1, "programming": 2, "music": 1 }
var list = _.map(data, function(element){ var name=element.name return _.map(element.favorites, function(favorite){ return {"name": name, "favorite": favorite} }) }) var groups = _.groupBy(_.flatten(list), function(d){ return d.favorite }) var result = _.mapValues(groups, function(value){ return value.length }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'travel'], city: 'Denver'}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel'], city: 'Boulder'}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening'], city: 'Denver'}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming'], city: 'Boulder'}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food'], city: 'Denver'}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music'], city: 'Denver'}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming'], city: 'Boulder'}]
{ "Denver": [ { "name": "John Smith", "age": 54, "favorites": [ "food", "travel" ], "city": "Denver" }, { "name": "Peter Pan", "age": 15, "favorites": [ "movies", "tv", "gardening" ], "city": "Denver" }, { "name": "Adam Potts", "age": 42, "favorites": [ "sports", "food" ], "city": "Denver" }, { "name": "Joe Johnson", "age": 46, "favorites": [ "tv", "sports", "music" ], "city": "Denver" } ], "Boulder": [ { "name": "Mary Smith", "age": 42, "favorites": [ "sports", "travel" ], "city": "Boulder" }, { "name": "Kelly Fan", "age": 35, "favorites": [ "movies", "travel", "programming" ], "city": "Boulder" }, { "name": "Ben Smith", "age": 35, "favorites": [ "movies", "tv", "programming" ], "city": "Boulder" } ] }
{ "Denver": [ { "name": "John Smith", "age": 54, "favorites": [ "food", "travel" ], "city": "Denver" }, { "name": "Peter Pan", "age": 15, "favorites": [ "movies", "tv", "gardening" ], "city": "Denver" }, { "name": "Adam Potts", "age": 42, "favorites": [ "sports", "food" ], "city": "Denver" }, { "name": "Joe Johnson", "age": 46, "favorites": [ "tv", "sports", "music" ], "city": "Denver" } ], "Boulder": [ { "name": "Mary Smith", "age": 42, "favorites": [ "sports", "travel" ], "city": "Boulder" }, { "name": "Kelly Fan", "age": 35, "favorites": [ "movies", "travel", "programming" ], "city": "Boulder" }, { "name": "Ben Smith", "age": 35, "favorites": [ "movies", "tv", "programming" ], "city": "Boulder" } ] }
var groups = _.groupBy(data, function(d){ return d.city }) return groups
[{name: 'John Smith', age: 54, favorites: ['food', 'travel'], city: 'Denver'}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel'], city: 'Boulder'}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening'], city: 'Denver'}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming'], city: 'Boulder'}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food'], city: 'Denver'}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music'], city: 'Denver'}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming'], city: 'Boulder'}]
{ "Denver": 4, "Boulder": 3 }
{ "Denver": 4, "Boulder": 3 }
var groups = _.groupBy(data, function(d){ return d.city }) var result = _.mapValues(groups, function(value){ return value.length }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'travel'], city: 'Denver'}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel'], city: 'Boulder'}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening'], city: 'Denver'}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming'], city: 'Boulder'}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food'], city: 'Denver'}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music'], city: 'Denver'}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming'], city: 'Boulder'}]
{ "Denver": 54, "Boulder": 42 }
{ "Denver": 54, "Boulder": 42 }
var groups = _.groupBy(data, function(d){ return d.city }) var result = _.mapValues(groups, function(value){ return _.max(value, function(person){ return person.age }).age }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'travel'], city: 'Denver'}, {name: 'Mary Smith', age: 42, favorites: ['sports', 'travel'], city: 'Boulder'}, {name: 'Peter Pan', age: 15, favorites: ['movies', 'tv', 'gardening'], city: 'Denver'}, {name: 'Kelly Fan', age: 35, favorites: ['movies', 'travel', 'programming'], city: 'Boulder'}, {name: 'Adam Potts', age: 42, favorites: ['sports', 'food'], city: 'Denver'}, {name: 'Joe Johnson', age: 46, favorites: ['tv', 'sports','music'], city: 'Denver'}, {name: 'Ben Smith', age: 35, favorites: ['movies', 'tv', 'programming'], city: 'Boulder'}]
{ "Denver": 1, "Boulder": 2 }
{ "Denver": 1, "Boulder": 2 }
var groups = _.groupBy(data, function(d){ return d.city }) var result = _.mapValues(groups, function(value){ return _.filter(value, function(person){ return _.includes(person.name, 'Smith') }).length }) return result