Underscore.js, remove duplicates in array of objects based on key value
The other answer is definitely best but here's another that's not much longer that also exposes you to more underscore method's, if you're interested in learning:
var mySubArray = []
_.each(_.uniq(_.pluck(myArray, 'name')), function(name) {
mySubArray.push(_.findWhere(myArray, {name: name}));
})
With Underscore, use _.uniq
with a custom transformation, a function like _.property('name')
would do nicely or just 'name'
, as @Gruff Bunny noted in the comments :
var mySubArray = _.uniq(myArray, 'name');
And a demo http://jsfiddle.net/nikoshr/02ugrbzr/
If you use Lodash and not Underscore, go with the example given by @Jacob van Lingen in the comments and use _.uniqBy:
var mySubArray = _.uniqBy(myArray, 'name')