簡體   English   中英

JavaScript:有條件地將JSON對象從一個數組添加到另一個數組

[英]JavaScript: Add JSON objects from one array to another array conditionally

我想計算合並JSON。 我有兩個JSON數組,想將對象從一個數組添加到另一個具有相同值的對象

產品展示

"Products": [
                {
                    "Date": "2015-04-28T12:30:19.107",
                    "Code": "003UYTX1A",
                    "Title": "Divatex Dots Microfiber Queen Bed In the Bag",
                    "Description": "Go crazy with lots of dots with Divatex,                    
                    "Weight": 7.60,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.1,                    
                    "EstimatedSalesPerDay": 761,
                    "ProfitScore": 50,
                    "UpdateStatus": 0,
                    "VersionCode": 23
                },
                {
                    "Date": "2015-04-28T12:29:46.66",
                    "Code": "05461AQDV",
                    "Title": "Wilton 1912-1294 100 Count Party Bags",
                    "Description": "Some Description,                    
                    "Weight": 8.10,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.0,                    
                    "EstimatedSalesPerDay": 711,
                    "ProfitScore": 45,
                    "UpdateStatus": 0,
                    "VersionCode": 23
                }]

詳情

"Detail": [
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "003UYTX1A",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Some Title",
                    "TotalReviews": 31
                },
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "05461AQDV",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Title",                                                
                    "TotalReviews": 211
                },
                {
                    "Date": "2015-04-28T12:29:45.95",
                    "Code": "003UYTX1A",
                    "CategoryId": "1055398",
                    "CategoryTitle": "Category Title",
                    "TotalReviews": 101
                }]

我需要在相同的Code值的基礎上合並這些數組對象,如下所示

      "Combined": [
               {
                    "Date": "2015-04-28T12:30:19.107",
                    "Code": "003UYTX1A",
                    "Title": "Divatex Dots Microfiber Queen Bed In the Bag",
                    "Description": "Go crazy with lots of dots with Divatex,                    
                    "Weight": 7.60,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.1,                    
                    "EstimatedSalesPerDay": 761,
                    "ProfitScore": 50,
                    "UpdateStatus": 0,
                    "VersionCode": 23,
                    "Detail":[
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "003UYTX1A",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Some Title",
                            "TotalReviews": 31
                        },
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "003UYTX1A",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Category Title",
                            "TotalReviews": 101
                        }]
                },
                {
                    "Date": "2015-04-28T12:29:46.66",
                    "Code": "05461AQDV",
                    "Title": "Wilton 1912-1294 100 Count Party Bags",
                    "Description": "Some Description,                    
                    "Weight": 8.10,
                    "WeightUnit": "pounds",                    
                    "AverageRating": 4.0,                    
                    "EstimatedSalesPerDay": 711,
                    "ProfitScore": 45,
                    "UpdateStatus": 0,
                    "VersionCode": 23,
                    "Detail":[
                        {
                            "Date": "2015-04-28T12:29:45.95",
                            "Code": "05461AQDV",
                            "CategoryId": "1055398",
                            "CategoryTitle": "Title",                                                
                            "TotalReviews": 211
                        }]
                }]

有獲得所需結果的最佳方法嗎? 我必須用JavaScript編寫代碼。

如果您使用下划線,則可以輕松實現, http://underscorejs.org/

 _.each(products,function(product,index){

        var detail = _.where(details,{'Code' : product.Code})
        if(detail){
                product.details=detail;
         }
         products[index] = product
    })

您可以嘗試以下方法:

var output = {Combined: []};
output.Combined = Products.map(function(product){
    var details = Detail.filter(function(detail){
        return detail.Code === product.Code
    }) || [];
    product.Detail = details;
    return product;
});

代碼的作用是遍歷您的Products ,找到可能為多個甚至為null的匹配Detail ,然后將其合並到關聯的產品對象中。

var jObject = {}; 

jObject['Combined'] = Products;

for(var i=0;i<Products.length;i++) {
    for(var j=0;j<Detail.length;j++){
        if(Products[i].Code === Detail[j].Code) {
            if(Products[i].Detail == undefined) {
                Products[i].Detail = [];
                Products[i].Detail.push(Detail[j]);
            } else {
                Products[i].Detail.push(Detail[j]);
            }
        }
    }   
}

暫無
暫無

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

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