JavaScript:有条件地将JSON对象从一个数组添加到另一个数组

时间:2015-06-05 09:28:58

标签: javascript arrays json

我想要合并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编写代码。

3 个答案:

答案 0 :(得分:4)

如果你使用下划线,你可以轻松地做到这一点, http://underscorejs.org/

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

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

答案 1 :(得分:2)

你可以试试这个:

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,找到匹配的Detail,它可能是多个甚至是null,并将其合并到您关联的产品对象。

答案 2 :(得分:2)

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