How can I turn this array of objects (which has some duplicate objects):
items =
[
{ TYPEID: 150927, NAME: 'Staples', COLOR: 'Silver' },
{ TYPEID: 1246007, NAME: 'Pencils', COLOR: 'Yellow' },
{ TYPEID: 1246007, NAME: 'Pencils', COLOR: 'Blue' },
{ TYPEID: 150927, NAME: 'Staples', COLOR: 'Black' },
{ TYPEID: 1248350, NAME: 'Staples', COLOR: 'Black' },
{ TYPEID: 1246007, NAME: 'Pencils', COLOR: 'Blue' },
{ TYPEID: 150927, NAME: 'Staples', COLOR: 'Silver' },
{ TYPEID: 150927, NAME: 'Fasteners', COLOR: 'Silver' }
]
Into this:
items =
[
{ TYPEID: 150927, NAME: 'Staples', COLOR: 'Silver' },
{ TYPEID: 1246007, NAME: 'Pencils', COLOR: 'Yellow' },
{ TYPEID: 1246007, NAME: 'Pencils', COLOR: 'Blue' },
{ TYPEID: 150927, NAME: 'Staples', COLOR: 'Black' },
{ TYPEID: 1248350, NAME: 'Staples', COLOR: 'Black' },
{ TYPEID: 150927, NAME: 'Fasteners', COLOR: 'Silver' }
]
...by filtering out the two duplicate objects (the silver staples and the blue pencils)?
It seems like there should be an easy way to do this, but I've yet to find a simple solution.
I've seen some javascript / jquery code which does this, but I'm not the best at converting those into coffeescript.
UPDATE:
There will often be different objects with the very similar properties.
In the actual application, each object has a potential of 25 properties.
I only want to remove objects if each of those properties are equal.
UPDATE 2
This is the code that ended up working for me - (thanks to rjz)
unique = (objAry, callback) ->
results = []
valMatch = (seen, obj) ->
for other in seen
match = true
for key, val of obj
match = false unless other[key] == val
return true if match
false
objAry.forEach (item) ->
unless valMatch(results, item)
results.push(item)
callback null, results
And this is how I'll call it:
unique items, (err, items) ->
console.log items