简体   繁体   中英

How can I check if the array of objects have same values

Here my array contains same address id but different phoneType and phonenumber. I need some help with iterating through array,i want to get phoneType and phonenumber when adressid is same. I keep getting stuck when comparing object values in array.

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"},
];

my expected out should be

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

Create an object where the addressID will be the key and the value of this key will be an array of objects which have this same key as the 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) 

Here is one way of doing it, first groupby the array, function is taken from the SO answer, refer here , then access whatever name you want, refer the below snippet!

 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; } 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM