Created
April 4, 2018 14:03
-
-
Save bduggan/6e75372b840256734634e585031e0c7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat unique.js | |
#!/usr/bin/env node | |
talks = [ | |
{ 'id' : 1, name : 'foo' }, | |
{ 'id' : 2, name : 'foo' }, | |
{ 'id' : 3, name : 'bar1' }, | |
{ 'id' : 3, name : 'bar2' }, | |
{ 'id' : 4, name : 'bar3' }, | |
{ 'id' : 5, name : 'bar4' } | |
] | |
var say = (e) => console.log(e) | |
function unique_by(attr,list) { | |
var seen = {} | |
return list.filter( ( {[attr] : id}, i) => { | |
seen[id] = seen[id] || 0 | |
return ! seen[id]++ | |
} ) | |
} | |
say( unique_by('id', talks) ) | |
say( unique_by('name', talks) ) | |
$ ./unique.js | |
[ { id: 1, name: 'foo' }, | |
{ id: 2, name: 'foo' }, | |
{ id: 3, name: 'bar1' }, | |
{ id: 4, name: 'bar3' }, | |
{ id: 5, name: 'bar4' } ] | |
[ { id: 1, name: 'foo' }, | |
{ id: 3, name: 'bar1' }, | |
{ id: 3, name: 'bar2' }, | |
{ id: 4, name: 'bar3' }, | |
{ id: 5, name: 'bar4' } ] | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment