Underscore: Remove object having some duplicate pr

2019-06-11 14:57发布

问题:

can somebody help me remove object from array having some duplicate properties.

var data = [{
    "IDPOSITION": "1",
    "LATITUDE": "5.35961",
    "LONGITUDE": "-3.10095",
    "IDUSAGER": "1",
    "DATECREATION": "2013-10-12 21:53:09"
}, {
    "IDPOSITION": "2",
    "LATITUDE": "5.35961",
    "LONGITUDE": "-4.00095",
    "IDUSAGER": "1",
    "DATECREATION": "2013-10-12 21:53:51"
}, {
    "IDPOSITION": "3",
    "LATITUDE": "5.35961",
    "LONGITUDE": "-4.00095",
    "IDUSAGER": "1",
    "DATECREATION": "2013-10-12 21:53:53"
}];

I need to remove objects having same couple of (LATITUDE, LONGITUDE). In the example above data[1] and data[2] are duplicate from criteras (LATITUDE, LONGITUDE)

回答1:

For example:

_.uniq(data, function(x) { return x.LATITUDE + "/" + x.LONGITUDE })

Basically, you provide a function that is supposed to return a hash value based on selected properties.