book

Lodash GroupBy

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.

Examples

Group people by age (teens, 20s, 30s, 40s, 50s)

Done
Data
[{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}]
Expected Output
{
  "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
    }
  ]
}
Actual Output
{
  "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
    }
  ]
}
Solution
var result = _.groupBy(data, function(d){
        return Math.floor(d.age / 10)    
    })
return result

What are the age groups?

Done
Data
[{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}]
Expected Output
[
  "1",
  "3",
  "4",
  "5"
]
Actual Output
[
  "1",
  "3",
  "4",
  "5"
]
Solution
var groups = _.groupBy(data, function(d){
        return Math.floor(d.age / 10)    
    })

var result = _.keys(groups)
return result

How many people are in each age group?

Done
Data
[{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}]
Expected Output
{
  "1": 1,
  "3": 2,
  "4": 3,
  "5": 1
}
Actual Output
{
  "1": 1,
  "3": 2,
  "4": 3,
  "5": 1
}
Solution
var groups = _.groupBy(data, function(d){
        return Math.floor(d.age / 10)    
    })

var result = _.mapValues(groups, function(value){
        return value.length
    })
return result

Exercises

Who is the first person in each age group?

Done
Data
[{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}]
Expected Output
{
  "1": "Peter Pan",
  "3": "Kelly Fan",
  "4": "Mary Smith",
  "5": "John Smith"
}
Actual Output
{
  "1": "Peter Pan",
  "3": "Kelly Fan",
  "4": "Mary Smith",
  "5": "John Smith"
}
Solution
var groups = _.groupBy(data, function(d){
        return Math.floor(d.age / 10)    
    })
var result = _.mapValues(groups, function(value){
        return value[0].name
})
return result

Group people by their last name

Done
Data
[{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}]
Expected Output
{
  "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
    }
  ]
}
Actual Output
{
  "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
    }
  ]
}
Solution
var results = _.groupBy(data, function(d){
    return d.name.split(' ')[1]
})
return results

How many people are in each last-name group?

Done
Data
[{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}]
Expected Output
{
  "Smith": 3,
  "Pan": 1,
  "Fan": 1,
  "Potts": 1,
  "Johnson": 1
}
Actual Output
{
  "Smith": 3,
  "Pan": 1,
  "Fan": 1,
  "Potts": 1,
  "Johnson": 1
}
Solution
var groups = _.groupBy(data, function(d){
    return d.name.split(' ')[1]
})
var result = _.mapValues(groups, function(value){
    return value.length
})
return result

Who is the first person in each last-name group?

Done
Data
[{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}]
Expected Output
{
  "Smith": "John Smith",
  "Pan": "Peter Pan",
  "Fan": "Kelly Fan",
  "Potts": "Adam Potts",
  "Johnson": "Joe Johnson"
}
Actual Output
{
  "Smith": "John Smith",
  "Pan": "Peter Pan",
  "Fan": "Kelly Fan",
  "Potts": "Adam Potts",
  "Johnson": "Joe Johnson"
}
Solution
var groups = _.groupBy(data, function(d){
    return d.name.split(' ')[1]
})
var result = _.mapValues(groups, function(value){
    return value[0].name
})
return result

What are all person-favorite pairs?

Done
Data
[{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']}]
Expected Output
[
  {
    "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"
  }
]
Actual Output
[
  {
    "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"
  }
]
Solution
// 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)

What are all age-favorite pairs (in ascending order)?

Done
Data
[{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']}]
Expected Output
[
  {
    "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"
  }
]
Actual Output
[
  {
    "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"
  }
]
Solution
// 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
})

Group people by their favorites.

Done
Data
[{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']}]
Expected Output
{
  "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"
    }
  ]
}
Actual Output
{
  "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"
    }
  ]
}
Solution
// 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

What are the names of the people in these 'favorite' groups?

Done
Data
[{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']}]
Expected Output
{
  "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"
  ]
}
Actual Output
{
  "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"
  ]
}
Solution
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

What are the sizes of these 'favorite' groups?

Done
Data
[{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']}]
Expected Output
{
  "food": 2,
  "travel": 3,
  "sports": 3,
  "movies": 3,
  "tv": 3,
  "gardening": 1,
  "programming": 2,
  "music": 1
}
Actual Output
{
  "food": 2,
  "travel": 3,
  "sports": 3,
  "movies": 3,
  "tv": 3,
  "gardening": 1,
  "programming": 2,
  "music": 1
}
Solution
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

Group people by city

Done
Data
[{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'}]
Expected Output
{
  "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"
    }
  ]
}
Actual Output
{
  "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"
    }
  ]
}
Solution
var groups = _.groupBy(data, function(d){
    return d.city
})
return groups

Group people by city and count how many people in each city

Done
Data
[{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'}]
Expected Output
{
  "Denver": 4,
  "Boulder": 3
}
Actual Output
{
  "Denver": 4,
  "Boulder": 3
}
Solution
var groups = _.groupBy(data, function(d){
    return d.city
})
var result = _.mapValues(groups, function(value){
        return value.length
    })
return result

What is the oldest age in each city?

Done
Data
[{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'}]
Expected Output
{
  "Denver": 54,
  "Boulder": 42
}
Actual Output
{
  "Denver": 54,
  "Boulder": 42
}
Solution
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

How many Smith's are in each city?

Done
Data
[{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'}]
Expected Output
{
  "Denver": 1,
  "Boulder": 2
}
Actual Output
{
  "Denver": 1,
  "Boulder": 2
}
Solution
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