删除数组中的重复项Javascript / AngularJS

时间:2017-05-01 23:50:17

标签: javascript angularjs sorting

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

[  
{  
  "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”,删除一个项目是因为它是相同的。

结果

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”项目。

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

有任何想法有效地做到这一点吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

使用下划线库(http://underscorejs.org/#uniq)中的uniq-function和迭代函数。

@Configuration
public class RestConfigs {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addExposedHeader(MyUtils.AUTHENTICATION); //Header String
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("DELETE");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }