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

1
  • "Any idea to do this efficiently?" What are the current benchmarks of the different javascript approaches that you tried to resolve inquiry? Commented May 2, 2017 at 0:00

1 Answer 1

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

})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Nice library! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.