0

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

3 Answers3

1

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)
brk
  • 48,835
  • 10
  • 56
  • 78
0

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;
}
Naren Murali
  • 19,250
  • 3
  • 27
  • 54
0

I prefer Lodash for things like this (data wrangling)...

var results = _.filter(userdetail, {name: 'Steve'});
Tallboy
  • 12,847
  • 13
  • 82
  • 173