1. Take this array of objects and extract out the values of a particular property from each of them.
Use can use this array:
var arrayObj = [{name: "Johen", age: 7}, {name : "Brian", age : 8}, {name: "Sam", age: 98}, {name : "Jules", age : 89}];
2. Build a function that will take an array of objects, and return the number of times a particular value in each object passes a truth test.
For example, let's say, in the above array named arrayObj, that I wanted to count how many people are over 18.
My function has three parameters, collection, key, age . So, I can put enter in any age I like, and get the number of people over that age returned to me, as a number. The above array returns 2 in my function if I use 18 as age.
Solution in the comments.
var countAge = function(array, key, age ){
ReplyDeletevar counter = 0;
if(_.each(_.pluck(array, key), function(item){
if (item > 18){
counter ++;
}
}));
return counter;
}