过滤键上的对象

时间:2018-03-26 10:15:44

标签: javascript

我使用GROUP CONCAT从SQL获取数据。 我最终想要实现的是输出预期。我尝试使用过滤器和地图,但无法实现所需的输出。 我们如何实现这一点,以便serviceObj包含对象数组

预期输出

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

2 个答案:

答案 0 :(得分:1)

如果您拆分了ID和名称,则可以map覆盖它们以创建一个serviceObj数组,您可以将其添加到返回的对象数组中:

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

答案 1 :(得分:0)

对于a数组中的每个项目,Array.prototype.split()上的services_idservices_name字段;。假设它们都具有相同的长度,并行地迭代它们并根据两个数组中的字段创建新的对象数组。



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