0

I want to search and delete duplicated items

[  
{  
  "id":"1",
  "rawId":"1",
  "displayName":"Asd",
  "name":{  
     "givenName":"Asd",
     "formatted":"Asd"
  },
  "nickname":null,
  "phoneNumbers":[  
     {  
        "id":"1",
        "pref":false,
        "value":"213213 414 86 86",
        "type":"mobile"
     }
  ],
  "emails":null
},
{  
  "id":"2",
  "rawId":"2",
  "displayName":"Bbb",
  "name":{  
     "givenName":"Bbb",
     "formatted":"Bbb"
  },
  "nickname":null,
  "phoneNumbers":[  
     {  
        "id":"3",
        "pref":false,
        "value":"565 65 65 123123",
        "type":"mobile"
     }
  ],
  "emails":null
},
{  
  "id":"3",
  "rawId":"3",
  "displayName":"Ccc",
  "name":{  
     "givenName":"Ccc",
     "formatted":"Ccc"
  },
  "nickname":null,
  "phoneNumbers":[  
     {  
        "id":"5",
        "pref":false,
        "value":"123 14 40 111",
        "type":"mobile"
     }
  ],
  "emails":null,
},
{  
  "id":"6",
  "rawId":"6",
  "displayName":"Nube",
  "name":{  
     "givenName":"Nube",
     "formatted":"Nube"
  },
  "nickname":null,
  "phoneNumbers":[  
     {  
        "id":"13",
        "pref":false,
        "value":"111 22 33",
        "type":"mobile"
     }
  ],
  "emails":null

},
{  
  "id":"8",
  "rawId":"6",
  "displayName":"Nube",
  "name":{  
     "givenName":"Nube",
     "formatted":"Nube"
  },
  "nickname":null,
  "phoneNumbers":[  
     {  
        "id":"13",
        "pref":false,
        "value":"111 22 33",
        "type":"mobile"
     }
  ],
  "emails":null

}
]

In this example the last item is repeated, I want to search in that array of objects if "name.formatted" and "name.phoneNumbers[0].value", delete one item because is the same.

As result

enter code here

[  
{  
"id":"1",
"rawId":"1",
"displayName":"Asd",
"name":{  
 "givenName":"Asd",
 "formatted":"Asd"
},
"nickname":null,
"phoneNumbers":[  
 {  
    "id":"1",
    "pref":false,
    "value":"213213 414 86 86",
    "type":"mobile"
 }
],
"emails":null
},
{  
 "id":"2",
  "rawId":"2",
  "displayName":"Bbb",
 "name":{  
   "givenName":"Bbb",
   "formatted":"Bbb"
},
 "nickname":null,
 "phoneNumbers":[  
 {  
    "id":"3",
    "pref":false,
    "value":"565 65 65 123123",
    "type":"mobile"
 }
  ],
"emails":null
},
{  
 "id":"3",
 "rawId":"3",
 "displayName":"Ccc",
 "name":{  
 "givenName":"Ccc",
 "formatted":"Ccc"
},
"nickname":null,
"phoneNumbers":[  
 {  
    "id":"5",
    "pref":false,
    "value":"123 14 40 111",
    "type":"mobile"
 }
],
"emails":null,
},
{  
  "id":"6",
  "rawId":"6",
 "displayName":"Nube",
 "name":{  
   "givenName":"Nube",
   "formatted":"Nube"
},
   "nickname":null,
  "phoneNumbers":[  
 {  
    "id":"13",
    "pref":false,
    "value":"111 22 33",
    "type":"mobile"
 }
],
 "emails":null

}
]

One "Nube" item was deleted.

Think that in this array will be around 700 objects and 40-60 items are duplicated (Same name and same phone).

Any idea to do this efficiently?

Lot of thanks

  • _"Any idea to do this efficiently?"_ What are the current benchmarks of the different `javascript` approaches that you tried to resolve inquiry? – guest271314 May 02 '17 at 00:00

1 Answers1

0

Use uniq-function at underscore-library (http://underscorejs.org/#uniq) with an iterate function.

var result = _.uniq(yourArray, function(item){

     return item.phoneNumbers && item.name.formatted

})