28

I am using _underscore.js to find all unique items in an array, but I can't figure out how to also get the number of unique items returned.

_PERSONARRAY = [{name:"tom",age:7}{name:"john",age:9}{name:"becky",age:2}{name:"sam",age:7}]

_UNIQUEAGEARRAY = _.chain(_PERSONARRAY).map(function(person) { return person.age }).uniq().value();

In this case _UNIQUEAGEARRAY will equal:

[7,9,2]

What I actually need returned is something like:

[{uniqueAge:7,numberOfPeople:2}{uniqueAge:9,numberOfPeople:1}{uniqueAge:2,numberOfPeople:1}]

Thanks for help. Also, I'm assuming _underscore.js is quick at doing this?? If it's stupid slow tell me cause I'd be open to other solutions.

4 Answers 4

82

A nice solution is to use the optional iterator function to underscore's uniq function:

let people = [
  {name: "Alice", age: 21}, 
  {name: "Bob", age: 34},
  {name: "Caroline", age: 21}
];
_.uniq(people, person => person.age);

Docs: http://underscorejs.org/#uniq

Sign up to request clarification or add additional context in comments.

Comments

17

I think you're looking for the countBy function:

_UNIQUEAGEARRAY = _.countBy(_PERSONARRAY, "age");

It produces the result:

{"2":1,"7":2,"9":1}

JSFiddle demo: http://jsfiddle.net/4J2SX/

2 Comments

Then you can simply run it through map to get it in the object structure you wanted (updated at jsfiddle.net/4J2SX/2 )
@howard suppose I have this array of objects: [{couponid: 500 locationid: 10 },{couponid: 600 locationid: 15 }, { couponid: 500 locationid: 10 }, { couponid: 500 locationid: 20 } ] Using the solution given by you, I got an array of Unique coupon ids, with counts for each unique coupon id; it was the most concise anwer I found after lot of searhcing;Can you please tell, now how can i get, for each unique couponid, how many unique location ids ? ie in the above couponid 500 would have 2, and couponid 600 would have 1. Shall I post this as a separate question ?
1

You can use underscore's groupBy if you want (might not be a good idea for a large dataset since it keeps a list of all the grouped items)

Example:

var d = _.groupBy(_PERSONARRAY, function(p){ 
    return p.age;
});

If you want to map this to your exact format try doing a map after the groupBy:

var x = _.map(d, function(people, age) {   
    return {uniqueAge: age,  numberOfPeople: people.length};   
});

jsFiddle: http://jsfiddle.net/jsgkC/2/

1 Comment

Note: groupBy, countBy, indexBy all use the same "group" function internally so I don't know if one is that much better than the other from a performance point of view. You can see the annotated source here: underscorejs.org/docs/underscore.html
0

groupBy function is useful.

_personsList = [{name:"anjo",age:5},{name:"george",age:3},{name:"jack",age:5}];
_uniqAgeList = _.groupBy(_personsList, "age");

will produce output

{
    "3":[{"name":"george","age":3}],
    "5":[{"name":"anjo","age":5},{"name":"jack","age":5}]
}

jsfiddle :http://jsfiddle.net/4J2SX/199/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.