簡體   English   中英

用嵌套對象(JS)重新排列數組

[英]Re-arrange an array with nested object (JS)

我從數據源收到以下活動響應。 這些具有唯一的開始時間,並且可以屬於同一產品。

當前,產品信息對象嵌套在活動中。 我嘗試“重新排列”和分組信息未成功。

我需要的結構是擁有一個Product(productCode),其活動與Product內的對象具有相同的產品代碼。 例如。 “ productCode”:“ PTFTVD”“ activity”:[{活動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"
  }
   }
]

基本上,數據交換分為兩個階段:

  1. 分組產品,
  2. 收集產品的活動。

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

如果您使用的是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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM