AngularJS中的重复项

AngularJS中的重复项

本文介绍了删除数组Javascript / AngularJS中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索和删除重复的项目

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

}
]

在这个例子中,最后一项是重复的,我想在name.formatted和name.phoneNumbers [0] .value中搜索该对象数组,删除一个项目,因为是相同的。

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.

结果

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

}
]

一个Nube项目已被删除。

One "Nube" item was deleted.

认为在此数组大约有700个对象,40-60个项目是重复的(同名和同一个手机)。

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?

非常感谢

推荐答案

使用uniq-function在下划线库()中使用迭代函数。

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

})

这篇关于删除数组Javascript / AngularJS中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:22