使用嵌套对象(JS)重新排列数组

时间:2017-09-05 06:21:41

标签: javascript lodash

我从数据源收到以下活动响应。它们具有独特的开始时间,并且可以属于同一产品。

目前,Product Information对象嵌套在activity中。我曾尝试过“重新安排”并对信息进行分组。

我需要的结构是让Product(productCode)在Product中的对象中包含具有相同产品代码的活动。  例如。 “产品代码”: “PTFTVD” “活动”:[{活动1,活动2等}]

 var activities = [  
   {  
  "id":39170350,
  "productCode":"PTFTVD",
  "startTime":"2017-09-06T00:00:00Z",
  "endTime":"2017-09-06T05:30:00Z",
  "startTimeLocal":"2017-09-06 10:00:00",
  "endTimeLocal":"2017-09-06 15:30:00",
  "product":{  
     "productCode":"PTFTVD",
     "productType":"DAYTOUR",
     "name":"01 Koala & River Cruise - Return cruise with Entry into Lone Pine",
     "shortDescription":"The Koala and River Cruise is a memorable"
  }
   },
{  
  "id":41498876,
  "productCode":"PJIOQO",
  "startTime":"2017-09-06T04:15:00Z",
  "discount":{  
     "id":7,
     "title":"Discount Rulezzz"
  },
  "product":{  
     "productCode":"PJIOQO",
     "productType":"CUSTOM",
     "name":"1 Hour 15 Minute Segway Joy Ride Experience",
     "shortDescription":"Tour Length 14km  approx. "
  }
   },
  {  
  "id":41498757,
  "productCode":"PJIOQO",
  "startTime":"2017-09-07T04:15:00Z",
  "product":{  
     "productCode":"PJIOQO",
     "productType":"CUSTOM",
     "name":"1 Hour 15 Minute Segway Joy Ride Experience",
     "shortDescription":"Tour Length 14km  approx. Almost non stop segway r…nd we custom make this tour to "
  }
   },
   {  
  "id":41498846,
  "productCode":"PJIOQO",
  "startTime":"2017-09-08T04:15:00Z",
  "product":{  
     "productCode":"PJIOQO",
     "productType":"CUSTOM",
     "name":"1 Hour 15 Minute Segway Joy Ride Experience",
     "shortDescription":"Tour Length 14km  approx. Almost non stop segway r…nd we custom"
  }
   },
   {  
  "id":41498600,
  "productCode":"PJIOQO",
  "startTime":"2017-09-09T04:15:00Z",
  "product":{  
     "productCode":"PJIOQO",
     "productType":"CUSTOM",
     "name":"1 Hour 15 Minute Segway Joy Ride Experience",
     "shortDescription":"Tour Length 14km  approx. Almost non stop segway r…nd we custom make this tour t"
  }
   }
]

2 个答案:

答案 0 :(得分:0)

基本上,数据切换分为两个阶段:

  1. 对产品进行分组,
  2. 收集产品的活动。
  3. 对于1,您需要一个可搜索的数据结构,如对象或Map,您可以在其中收集密钥和数据。

    在此,您可以使用productCode作为关键字,并将product的数据作为新值。然后添加一个用于收集活动的属性。

    对结果集使用数组并将新产品推送到结果集,同时仍可使用对象中的键访问产品。

    现在转到2.收集所有数据并将其分配给活动数组。

    瞧!

    
    
    var activities = [{ id: 39170350, productCode: "PTFTVD", startTime: "2017-09-06T00:00:00Z", endTime: "2017-09-06T05:30:00Z", startTimeLocal: "2017-09-06 10:00:00", endTimeLocal: "2017-09-06 15:30:00", product: { productCode: "PTFTVD", productType: "DAYTOUR", name: "01 Koala & River Cruise - Return cruise with Entry into Lone Pine", shortDescription: "The Koala and River Cruise is a memorable" } }, { id: 41498876, productCode: "PJIOQO", startTime: "2017-09-06T04:15:00Z", discount: { id: 7, title: "Discount Rulezzz" }, product: { productCode: "PJIOQO", productType: "CUSTOM", name: "1 Hour 15 Minute Segway Joy Ride Experience", shortDescription: "Tour Length 14km  approx. " } }, { id: 41498757, productCode: "PJIOQO", startTime: "2017-09-07T04:15:00Z", product: { productCode: "PJIOQO", productType: "CUSTOM", name: "1 Hour 15 Minute Segway Joy Ride Experience", shortDescription: "Tour Length 14km  approx. Almost non stop segway r…nd we custom make this tour to " } }, { id: 41498846, productCode: "PJIOQO", startTime: "2017-09-08T04:15:00Z", product: { productCode: "PJIOQO", productType: "CUSTOM", name: "1 Hour 15 Minute Segway Joy Ride Experience", shortDescription: "Tour Length 14km  approx. Almost non stop segway r…nd we custom" } }, { id: 41498600, productCode: "PJIOQO", startTime: "2017-09-09T04:15:00Z", product: { productCode: "PJIOQO", productType: "CUSTOM", name: "1 Hour 15 Minute Segway Joy Ride Experience", shortDescription: "Tour Length 14km  approx. Almost non stop segway r…nd we custom make this tour t" } }],
        hash = Object.create(null),
        products = [];
    
    activities.forEach(function (a) {
        var temp = {},
            key = a.product.productCode;
        if (!hash[key]) {
            hash[key] = {};
            Object.keys(a.product).forEach(function (k) {
                hash[key][k] = a.product[k];
            });
            products.push(hash[key]);
            hash[key].activities = [];
        }
        Object.keys(a).forEach(function (k) {
            if (k !== 'product') {
                temp[k] = a[k];
            }
        });
        hash[key].activities.push(temp);
    });
    
    console.log(products);
    
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    
    
    

答案 1 :(得分:0)

如果您使用的是ES6,则可以执行以下操作

 ....
 //activities already defined
 let projectToActivityObject = {}
 activities.forEach((activity) => {
    let productID = activity.product.productCode;
    projectToActivityObject[productID] = projectToActivityObject.hasOwnProperty(productID) ? projectToActivityObject[productID] : new Set();
    projectToActivityObject[productID].add(activity);
 });
 console.log(projectToActivityObject);