Reputation: 536
I would like to count all the companies in the following JSON string. I was first planning on doing it manually with loops but it seems like this is a good chance for me to learn to use map/reduce... How can return something that returns {company: 2}
?
[
{
_id: '123',
company: 'Acme'
}
{
_id: '123',
company: 'Innatrode'
}
{
_id: '123',
company: 'Acme'
}
]
Upvotes: 1
Views: 39
Reputation: 239573
If you want to get the number of unique company names, then
use _.pluck
to get the company
attribute out of the list of company objects
Find the unique values out of them with _.uniq
, which will return an array.
Compose a new object with the key `com
console.log({company: _.uniq(_.pluck(companies, "company")).length});
# { company: 2 }
Upvotes: 2