Weird result when Underscore.js, remove duplicates in array of objects based on key value

i have some weird result when i try to remove duplicates from an array of objects, let me explain this is my array is returned by a service:

var mens =[{
    "text":"Ahoy",
    "from":"9728467",
    "to":"9732825",
    "date":"2017-05-19 18:37:14",
    "idProyecto":"1",
    "foto":null,
    "fotolocal":null,
    "tipo":1,
    "sync":1,
    "privado":1,
    "descargado":0,
    "miembros":null,
    "idSyncLocal":"97284672017519183714",
    "$$hashKey":"object:96"
},
{
    "text":"Salut",
    "from":"9728467",
    "to":"9732825",
    "date":"2017-05-19 18:37:26",
    "idProyecto":"1",
    "foto":null,
    "fotolocal":null,
    "tipo":1,
    "sync":1,
    "privado":1,
    "descargado":0,
    "miembros":null,
    "idSyncLocal":"97284672017519183726",
    "$$hashKey":"object:108"
},
{
    "text":"1",
    "from":"9728467",
    "to":"9732825",
    "date":"2017-05-19 18:37:39 ",
    "idProyecto":1,
    "foto":null,
    "fotolocal":null,
    "tipo":1,
    "sync":1,
    "privado":1,
    "descargado":1,
    "miembros":null,
    "IdSyncLocal":"97284672017519183739"
},
{
    "text":"2",
    "from":"9728467",
    "to":"9732825",
    "date":"2017-05-19 18:37:44 ",
    "idProyecto":1,
    "foto":null,
    "fotolocal":null,
    "tipo":1,
    "sync":1,
    "privado":1,
    "descargado":1,
    "miembros":null,
    "IdSyncLocal":"97284672017519183744"
},
{
    "text":"3",
    "from":"9728467",
    "to":"9732825",
    "date":"2017-05-19 18:37:51 ",
    "idProyecto":1,
    "foto":null,
    "fotolocal":null,
    "tipo":1,
    "sync":1,
    "privado":1,
    "descargado":1,
    "miembros":null,
    "IdSyncLocal":"97284672017519183751"
}]

and i have this code:

var mySubArray = _.uniq(mens, 'IdSyncLocal');
document.getElementById('content').innerHTML += '<br>Some new content!' +        JSON.stringify(mySubArray);

The thing is it should return the not repeated items filtered by IdSyncLocal, but it always removes the second item, in this example all the items are different, so i dont know why is returning bad, please if someone could help me with finding some answer, i will be very thankful.

Upvotes: 0

Views: 166

Answers (1)

Karin
Karin

Reputation: 8610

The first two items in the list have keys that are idSyncLocal instead of IdSyncLocal (note capitalization). Therefore, the value of IdSyncLocal for both is undefined, which is why they are considered duplicates.

Upvotes: 1

Related Questions