从对象数组中获取所有重复值

时间:2015-06-25 15:34:36

标签: javascript jquery json

我从JSON数据中反序列化了以下对象



[{
  "EmployeeNumber": "169",
  "EmployeeNumberError": "",
  "EmployeeFirstName": "saj"
}, {
  "EmployeeNumber": "169",
  "EmployeeNumberError": "",
  "EmployeeFirstName": "Carlton"
}, {
  "EmployeeNumber": "1002",
  "EmployeeNumberError": "",
  "EmployeeFirstName": "Stanley"
}]




我希望得到所有重复的' EmployeeNumber'从使用javascript或jQuery的对象。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个

$(document).ready(function() {
  var v = [{
    "EmployeeNumber": "169",
    "EmployeeNumberError": "",
    "EmployeeFirstName": "saj"
  }, {
    "EmployeeNumber": "169",
    "EmployeeNumberError": "",
    "EmployeeFirstName": "Carlton"
  }, {
    "EmployeeNumber": "1002",
    "EmployeeNumberError": "",
    "EmployeeFirstName": "Stanley"
  }];

  var a = [];
  for (x = 0; x < v.length; x++) {
    for (y = 0; y < v.length; y++) {
      if (v[x].EmployeeNumber == v[y].EmployeeNumber) {
        a.push(v[x].EmployeeNumber)
      }
    }

  }
  a = jQuery.unique(a);
  alert(a[1]);


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>