合并相同的JSON元素

时间:2017-10-10 01:35:12

标签: javascript arrays json

如何合并此JSON数据。简单地合并相同的对象并将不同的“product_code”添加到依赖于“product_web_name”的相同JSON元素的数组中。

{
    "search_result": {
        "results": [
            {
                "product_id": "1",
                "product_code": "Aa",
                "product_description": "test desc a",
                "product_web_name": "Product A"

            },
            {
                "product_id": "2",
                "product_code": "Bb111",
                "product_description": "test desc b",
                "product_web_name": "Product B",

            },
            {
                "product_id": "2",
                "product_code": "Bb222",
                "product_description": "test desc b",
                "product_web_name": "Product B"
            },
            {
                "product_id": "2",
                "product_code": "Bb333",
                "product_description": "test desc b",
                "product_web_name": "Product B",

            }
        ]
    }
}

最终输出结果或结果将是这样的

{
        "search_result": {
            "results": [
                {
                    "product_id": "1",
                    "product_code": "Aa",
                    "product_description": "test desc a",
                    "product_web_name": "Product A"

                },
                {
                    "product_id": "2",
                    "product_code": [
                        {
                            "product_code_id":"1",
                            "product_code": "Bb111"
                        },
                        {
                            "product_code_id":"2",
                            "product_code": "Bb222"
                        },
                        {
                            "product_code_id":"3",
                            "product_code": "Bb333"
                        }
                     ],
                    "product_description": "test desc b",
                    "product_web_name": "Product B",

                }
            ]
        }
    }

我想合并JSON数据并添加相同的元素。到目前为止,我已尝试过该代码。

var mergethis = {};
                            data = result.search_result.results.filter(function(entry) {
                                var previous;

                                if(mergethis.hasOwnProperty(entry.product_web_name)) {

                                    previous = mergethis[entry.product_web_name];
                                    previous.data.push(entry.data);


                                    return false;
                                }


                                if (!Array.isArray(entry.data)) {
                                    entry.data = [entry.data];
                                }

                                mergethis[entry.product_web_name] = entry;


                                return true;
                            });

                            console.log(mergethis);

2 个答案:

答案 0 :(得分:1)

这是下面的一个片段,可以帮助您开始,您可以根据需要按摩输出。所有这些都来自于使用Array.prototype.reduce Array.prototype.mapObject.prototype.keys。如果你看一下lodash或下划线,实用工具也可能有助于简化



var data = {
    "search_result": {
        "results": [
            {
                "product_id": "1",
                "product_code": "Aa",
                "product_description": "test desc a",
                "product_web_name": "Product A"

            },
            {
                "product_id": "2",
                "product_code": "Bb111",
                "product_description": "test desc b",
                "product_web_name": "Product B",

            },
            {
                "product_id": "2",
                "product_code": "Bb222",
                "product_description": "test desc b",
                "product_web_name": "Product B"
            },
            {
                "product_id": "2",
                "product_code": "Bb333",
                "product_description": "test desc b",
                "product_web_name": "Product B",

            }
        ]
    }
};

var grouped = data.search_result.results.reduce(function(acc, value) {
    var groupArray = acc[value.product_id];
    if (!groupArray) {
      groupArray = [];
      acc[value.product_id] = groupArray;
    }
    
    groupArray.push(value);
    
    return acc;
}, {});

//console.log(grouped);

data.search_result.results = Object.keys(grouped).map(function(key) {
  //return grouped[key][0];
  return {
    "product_id": grouped[key][0].product_id,
    "product_description": grouped[key][0].product_description,
    "product_code": grouped[key].length === 1 ? grouped[key][0].product_code : grouped[key]
  };
});

console.log(data);




答案 1 :(得分:0)

for(var i = 0 ; i < a.search_result.results.length; i++){
    switch(a.search_result.results[i].product_web_name){
        case 'Product A':
            a.search_result.results[i].product_code = 'something1';
            break;
        case 'Product B':
            a.search_result.results[i].product_code = 'something2';
            break;
        default :
            a.search_result.results[i].product_code = 'something3';

    }
}