Complete the following Lodash exercises. The goal is to become really good at
functional programming paradigm (e.g., _.map
, _.filter
, _.all
, _.any
...etc) and
a number of really useful Lodash methods (e.g., _.find
, _.pluck
... etc).
solution
blockfor/while
loop is allowedFamiliarity with programming in this way will not only make you a super productive programmer but also will pave the way for you to learn MapReduce and MongoDB.
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
4
4
return data.length
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
[ "John", "Mary", "Joe", "Ben" ]
[ "John", "Mary", "Joe", "Ben" ]
return _.map(data, function(d){ return d.name })
[{name: 'John'}, {name: 'Mary'}, {name: 'Joe'}, {name: 'Ben'}]
[ "John", "Joe" ]
[ "John", "Joe" ]
var list = _.filter(data, function(element){ var name=element.name return name.charAt(0) == 'J' }) var result=_.map(list, function(element){ return element.name }) return result
[{name: 'John'}, {name: 'John'}, {name: 'John'}, {name: 'Ben'}]
3
3
var list = _.filter(data, function(element){ var name=element.name return name == 'John' }) return _.size(list)
[{name: 'John Smith'}, {name: 'Mary Kay'}, {name: 'Peter Pan'}, {name: 'Ben Franklin'}]
[ "John", "Mary", "Peter", "Ben" ]
[ "John", "Mary", "Peter", "Ben" ]
var list = _.map(data, function(element){ var name=element.name.split(" ") return name[0] }) return list
[{name: 'John Smith'}, {name: 'Mary Smith'}, {name: 'Peter Pan'}, {name: 'Ben Smith'}]
[ "John", "Mary", "Ben" ]
[ "John", "Mary", "Ben" ]
var list = _.filter(data, function(element){ var name=element.name.split(" ") return name[1] == 'Smith' }) var result = _.map(list, function(element){ var name=element.name.split(" ") return name[0] }) return result
[{name: 'John Smith'}, {name: 'Mary Kay'}, {name: 'Peter Pan'}]
[ { "name": "Smith, John" }, { "name": "Kay, Mary" }, { "name": "Pan, Peter" } ]
[ { "name": "Smith, John" }, { "name": "Kay, Mary" }, { "name": "Pan, Peter" } ]
var result = _.map(data, function(element){ var name=element.name.split(" ") return {"name": name[1]+", "+name[0]} }) return result
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
1
1
var list = _.filter(data, function(element){ return element.gender == 'f' }) return _.size(list)
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
2
2
var list = _.filter(data, function(element){ var lastName=element.name.split(" ")[1] return element.gender == 'm' && lastName == 'Smith' }) return _.size(list)
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
true
true
var listM = _.filter(data, function(element){ return element.gender == 'm' }) var listF = _.filter(data, function(element){ return element.gender == 'f' }) return _.size(listM) > _.size(listF)
[{name: 'John Smith', gender: 'm'}, {name: 'Mary Smith', gender: 'f'}, {name: 'Peter Pan', gender: 'm'}, {name: 'Ben Smith', gender: 'm'}]
"m"
"m"
var list = _.filter(data, function(element){ return element.name == 'Peter Pan' }) return list[0].gender
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
54
54
var ages = _.map(data, function(element){ return element.age }) return _.max(ages)
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
true
true
// use _.all var ages = _.filter(data, function(element){ return element.age > 60 }) return _.size(ages) == 0
[{name: 'John Smith', age: 54}, {name: 'Mary Smith', age: 42}, {name: 'Peter Pan', age: 15}, {name: 'Ben Smith', age: 35}]
true
true
// use _.some var ages = _.filter(data, function(element){ return element.age < 18 }) return _.size(ages) > 0
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']}, {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']}, {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
3
3
var result = _.filter(data, function(element){ var food=_.filter(element.favorites, function(favorite){ return favorite == 'food' }) return _.size(food) > 0 }) return _.size(result)
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']}, {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']}, {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']}, {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
[ "Mary Smith", "Joe Johnson" ]
[ "Mary Smith", "Joe Johnson" ]
var list = _.filter(data, function(element){ var travel=_.filter(element.favorites, function(favorite){ return favorite == 'travel' }) return _.size(travel) > 0 && element.age > 40 }) var result = _.map(list, function(element){ return element.name }) return result
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']}, {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']}, {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']}, {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
"John Smith"
"John Smith"
var list = _.filter(data, function(element){ var food=_.filter(element.favorites, function(favorite){ return favorite == 'food' }) return _.size(food) > 0 }) var person = _.max(list, function(element){ return element.age }) return person.name
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']}, {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']}, {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']}, {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
[ "food", "movies", "travel", "minecraft", "pokemo", "craft" ]
[ "food", "movies", "travel", "minecraft", "pokemo", "craft" ]
// hint: use _.pluck, _.uniq, _.flatten in some order var favorites = _.pluck(data, 'favorites') var favList=_.flatten(favorites) var uniqList=_.uniq(favList) return uniqList
[{name: 'John Smith', age: 54, favorites: ['food', 'movies']}, {name: 'Mary Smith', age: 42, favorites: ['food', 'travel']}, {name: 'Peter Pan', age: 15, favorites: ['minecraft', 'pokemo']}, {name: 'Joe Johnson', age: 46, favorites: ['travel', 'movies']}, {name: 'Ben Smith', age: 35, favorites: ['craft', 'food']}]
[ "Smith", "Pan", "Johnson" ]
[ "Smith", "Pan", "Johnson" ]
var list = _.map(data, function(element){ return element.name.split(' ')[1] }) return _.uniq(list)