2017-09-05 71 views
1

我收到來自數據源的以下活動響應。這些具有獨特的開始時間,並且可以屬於同一產品。使用嵌套對象(JS)重新排列數組

當前產品信息對象嵌套在活動中。我試圖「重新安排」並對信息進行分組,嘗試失敗。

我需要的結構是在產品中的對象中包含具有相同產品代碼的活動的產品(productCode)。 例如。 「PRODUCTCODE」: 「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" 
    } 
    } 
] 
+0

很大,什麼不起作用?請添加您的代碼。 –

+0

首先在產品代碼中使用Lodash中的_.groupBy函數,這應該讓你開始,它會給你一個像'{「PJIOQO」:[products],...}的對象' –

回答

0

基本上數據的切換在兩個階段中進行分組:

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

+0

這適用於示例數據。不過,我之前遇到了絆腳石。 要創建活動數組的列表,我正在使用this.Activities.push(session); 其中,會話是個人活動。我需要這樣做,因爲我們過濾掉其他會話 當運行上述時,上述解決方案是空的。我是否推動會議進入錯誤的事情? – ChrisR

+0

與數據,我只能猜測,你的意思。 「this.Activities」和「session」是什麼? –

+0

我們做一個API調用,獲得一系列活動,我們稱之爲'activityResults' 然後,我們有邏輯循環遍歷'activityResults'並應用我們自己的邏輯以及符合我們條件的邏輯,我們將其推送到'activities' 我們沿着 'activities = []; (var x = 0; x ChrisR

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