Skip to content

Instantly share code, notes, and snippets.

@bduggan
Created April 4, 2018 14:03
Show Gist options
  • Save bduggan/6e75372b840256734634e585031e0c7c to your computer and use it in GitHub Desktop.
Save bduggan/6e75372b840256734634e585031e0c7c to your computer and use it in GitHub Desktop.
$ 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