score:2

Disclaimer: I wrote this library

You could use the javascript loq library to achieve this. Here's a snippet to get you on your way:

var list = [
  {name:"dog",type:"animal"},
  {name:"cat",type:"animal"},
  {name:"snake",type:"animal"},
  {name:"table",type:"furniture"},
  {name:"chair",type:"furniture"},
];
var filtered = loq(list).distinctBy(item => item.type);
console.log(filtered.toArray());
<script src="https://cdn.rawgit.com/biggyspender/loq/67db7014/lib/loq.min.js"></script>

...or with requirejs

requirejs.config({
    paths: {
        loq: 'https://cdn.rawgit.com/biggyspender/loq/67db7014/lib/loq.min'
    }
});

require(['loq'], function(loq){
  var list = [
    {name:"dog",type:"animal"},
    {name:"cat",type:"animal"},
    {name:"snake",type:"animal"},
    {name:"table",type:"furniture"},
    {name:"chair",type:"furniture"},
  ];
  var filtered = loq(list).distinctBy(item => item.type);
  console.log(filtered.toArray());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.js"></script>

It's a relatively complete reimplementation of LINQ-to-objects on iterables (objects that implement Symbol.iterator) with a few extra goodies (such as distinctBy). Take a look at the tests to see other uses.


Related Articles