如何检查对象数组是否具有相同的值

时间:2017-12-30 05:23:17

标签: javascript jquery

这里我的数组包含相同的地址ID但不同的phoneType和phonenumber。 我需要一些迭代数组的帮助,我希望在adressid相同时获得phoneType和phonenumber。在比较数组中的对象值时,我一直陷入困境。

var userdetail = [
    { name: "Steve",   adressID: "1", phoneType:"main",  phonenumber:"12222228"},
    { name: "Steve",   adressID: "1", phoneType:"fax" ,  phonenumber:"55555668"},
    { name: "Peter",   adressID: "2", phoneType:"main",  phonenumber:"67544442"},
    { name: "Elaine",  adressID: "3", phoneType:"main",  phonenumber:"87877778"},
    { name: "Elaine",  adressID: "3", phoneType:"mobile",phonenumber:"23234678"},
    { name: "Steve",   adressID: "1", phoneType:"work",  phonenumber:"42222228"},
];

我的预期应该是

     {
  "name": "Steve",
  "adressID": "1",
  "phoneType": "main",
  "phonenumber": "12222228"
},
{
  "name": "Steve",
  "adressID": "1",
  "phoneType": "fax",
  "phonenumber": "55555668"
},
{
  "name": "Steve",
  "adressID": "1",
  "phoneType": "work",
  "phonenumber": "42222228"
}

3 个答案:

答案 0 :(得分:1)

创建一个对象,其中addressID将是键,此键的值将是与addressID具有相同键的对象数组



var userdetail = [{
    name: "Steve",
    adressID: "1",
    phoneType: "main",
    phonenumber: "12222228"
  },
  {
    name: "Steve",
    adressID: "1",
    phoneType: "fax",
    phonenumber: "55555668"
  },
  {
    name: "Peter",
    adressID: "2",
    phoneType: "main",
    phonenumber: "67544442"
  },
  {
    name: "Elaine",
    adressID: "3",
    phoneType: "main",
    phonenumber: "87877778"
  },
  {
    name: "Elaine",
    adressID: "3",
    phoneType: "mobile",
    phonenumber: "23234678"
  },
  {
    name: "Steve",
    adressID: "1",
    phoneType: "work",
    phonenumber: "42222228"
  },
];
var result = userdetail.reduce(function(res, o) {
  res[o.adressID] || (res[o.adressID] = []); // if the adressID key doesn't exist, set it to be an array

  res[o.adressID].push(o); // push the object into the array

  return res;
}, {});
console.log(result)




答案 1 :(得分:0)

这是一种方法,第一组数组,函数取自SO答案,参考here,然后访问你想要的任何名称,参考下面的片段!

var userdetail = [{
    name: "Steve",
    adressID: "1",
    phoneType: "main",
    phonenumber: "12222228"
  },
  {
    name: "Steve",
    adressID: "1",
    phoneType: "fax",
    phonenumber: "55555668"
  },
  {
    name: "Peter",
    adressID: "2",
    phoneType: "main",
    phonenumber: "67544442"
  },
  {
    name: "Elaine",
    adressID: "3",
    phoneType: "main",
    phonenumber: "87877778"
  },
  {
    name: "Elaine",
    adressID: "3",
    phoneType: "mobile",
    phonenumber: "23234678"
  },
  {
    name: "Steve",
    adressID: "1",
    phoneType: "work",
    phonenumber: "42222228"
  },
];

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};

var data = groupBy(userdetail, 'name');
console.log(data);

//access values for steve
for (var k of data['Steve']) {
  console.log(k['phonenumber']);
  console.log(k['phoneType']);
}
.as-console {
  height: 100%;
}

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

答案 2 :(得分:0)

我更喜欢Lodash这样的事情(数据争论)......

var results = _.filter(userdetail, {name: 'Steve'});