Rohan Singh
Rohan Singh

Reputation: 91

Filtering object on keys

I am getting the data from SQL using GROUP CONCAT. What I finally want to achieve is Output expected. I tried using filter and map but couldn't achieve the desired output. How can we achieve this so as to get serviceObj contain the array of objects

Output expected

var a = [
    {
        "id": 1,
        "membership_name": "basic",
        "membership_price": "10",
        "serviceObj" :[
          {id :7, name:Multi-City Artisan Availability}, {id:3,name:Hair and/or Makeup},{id:6,Online Booking. Easy},{id:5, name:On Location. Whenever. Wherever},{id:4,name:2 Services / Month with Rollover}
       ],
} ..so on for 2 ids ]

INPUT

var a = [
    {
        "id": 1,
        "membership_name": "basic",
        "membership_price": "10",
        "services_id": "7;3;6;5;4",
        "services_names": "Multi-City Artisan Availability;Hair and/or Makeup;Online Booking. Easy;On Location. Whenever. Wherever;2 Services / Month with Rollover"
    },
    {
        "id": 2,
        "membership_name": "Elite",
        "membership_price": "123",
        "services_id": "10;9;12;8;11",
        "services_names": "2 Services / Month with Rollover;Hair and/or Makeup;Online Booking. Easy;Personal Makeup Shopper (1 appt);On Location. Whenever. Wherever."
    },
    {
        "id": 3,
        "membership_name": "Exclusive",
        "membership_price": "169",
        "services_id": "14;17;13;20;16;19;15;18",
        "services_names": "2 Services / Month with Rollover;Online Booking. Easy;Hair and/or Makeup;Choice of Updo / Downdo Hairstyle;On Location. Whenever. Wherever;Faux Lashes & Airbrush Included;Personal Makeup Shopper (1 appt);Multi-City Artisan Availability"
    },
    {
        "id": 4,
        "membership_name": "Life",
        "membership_price": "7999.20",
        "services_id": "21;30;25;29;24;27;23;26",
        "services_names": "VALID FOR LIFE!;Personalized Customer Care;Online Booking. Easy.;Choice of Updo / Downdo Hairstyle;On Location. Whenever. Wherever.;Faux Lashes & Airbrush Included;Hair and/or Makeup **;Multi-City Artisan Availability"
    }
];

var obj = {};
var k = [];
l = a.map(n=>{

  var obj = {
    id : n.id,
    membership_name : n.membership_name,
    membership_price : n.membership_price,
    service : [
      {services_id : n.services_id,services_names:n.services_names }
      ]
  }
  k.push(obj);
})

console.log(JSON.stringify(k));

Upvotes: 0

Views: 76

Answers (2)

wscourge
wscourge

Reputation: 11281

For every item in an a array, Array.prototype.split() its services_id and services_name fields on the ;. Assuming they both will have the same length, iterate both of them in parallel and create new array of objects, based on the fields from both of the arrays.

var a = [
    {
        "id": 1,
        "membership_name": "basic",
        "membership_price": "10",
        "services_id": "7;3;6;5;4",
        "services_names": "Multi-City Artisan Availability;Hair and/or Makeup;Online Booking. Easy;On Location. Whenever. Wherever;2 Services / Month with Rollover"
    },
    {
        "id": 2,
        "membership_name": "Elite",
        "membership_price": "123",
        "services_id": "10;9;12;8;11",
        "services_names": "2 Services / Month with Rollover;Hair and/or Makeup;Online Booking. Easy;Personal Makeup Shopper (1 appt);On Location. Whenever. Wherever."
    },
    {
        "id": 3,
        "membership_name": "Exclusive",
        "membership_price": "169",
        "services_id": "14;17;13;20;16;19;15;18",
        "services_names": "2 Services / Month with Rollover;Online Booking. Easy;Hair and/or Makeup;Choice of Updo / Downdo Hairstyle;On Location. Whenever. Wherever;Faux Lashes & Airbrush Included;Personal Makeup Shopper (1 appt);Multi-City Artisan Availability"
    },
    {
        "id": 4,
        "membership_name": "Life",
        "membership_price": "7999.20",
        "services_id": "21;30;25;29;24;27;23;26",
        "services_names": "VALID FOR LIFE!;Personalized Customer Care;Online Booking. Easy.;Choice of Updo / Downdo Hairstyle;On Location. Whenever. Wherever.;Faux Lashes & Airbrush Included;Hair and/or Makeup **;Multi-City Artisan Availability"
    }
];

var output = a.map((item) => {

    var ids   = item.services_id.split(';');
    var names = item.services_names.split(';');
    
    var objects = [];
    
    ids.forEach((id, index) => {
       
        objects.push({
          id  : parseInt(id),
          name: names[index]
        });
      
    });
    
    return {
      "id": item.id,
      "membership_name": item.membership_name,
      "membership_price": item.membership_price,
      "serviceObj": objects
    }

});

console.log(output);

Upvotes: 0

Andy
Andy

Reputation: 63524

If you split out the ids and names you can then map over them to create a serviceObj array that you can add to the returned object array:

const out = a.map(({ id, membership_name, membership_price, services_id, services_names }) => {
  const ids = services_id.split(';');
  const names = services_names.split(';');
  const serviceObj = ids.map((id, i) => ({ id: Number(id), name: names[i]}));
  return { id, membership_name, membership_price, serviceObj }
});

Upvotes: 1

Related Questions