如何使用JavaScript合并JSON对象?

时间:2015-01-26 04:30:33

标签: javascript json sorting

如何使用plain(不带jQuery)JavaScript合并JSON对象?

要求是:

转换自:

chartData=[
      {"date":"2014-05-1","CAT1":0.1},
      {"date":"2014-05-1","CAT2":0.2},
      {"date":"2014-05-1","CAT3":0.3},
      {"date":"2014-05-1","UNSET":0.4},
      {"date":"2014-05-2","CAT1":0.4},
      {"date":"2014-05-2","CAT2":0.3},
      {"date":"2014-05-2","CAT3":0.2},
      {"date":"2014-05-2","UNSET":0.1}
];

转换为:

chartData=[
    {"date":"2014-05-1","CAT1":0.1,"CAT2":0.2,"CAT3":0.3,"UNSET":0.4},           
    {"date":"2014-05-2","CAT1":0.4,"CAT2":0.3,"CAT3":0.2,"UNSET":0.1}
]

4 个答案:

答案 0 :(得分:2)

这里我们向后遍历在适当位置操作的chartData数组,并在合并内容时将元素拼接出数组:

var chartData=[{"date":"2014-05-1","CAT1":0.1},{"date":"2014-05-1","CAT2":0.2},{"date":"2014-05-1","CAT3":0.3},{"date":"2014-05-1","UNSET":0.4},  {"date":"2014-05-2","CAT1":0.4},{"date":"2014-05-2","CAT2":0.3},{"date":"2014-05-2","CAT3":0.2},{"date":"2014-05-2","UNSET":0.1}];

var chartDates = {}; /* stores references to elements for each date */
for (var i=chartData.length-1; i >= 0; i--) { 
    var date = chartData[i]['date'];
    if (date in chartDates) { 
        for (var k in chartData[i]) { 
            chartDates[date][k] = chartData[i][k];
        }
        chartData.splice(i,1);
    } else { 
        chartDates[date] = chartData[i];
    }
}
console.log(chartData);

答案 1 :(得分:1)

以下是如何执行此操作的示例...无需jquery。

chartData=[{"date":"2014-05-1","CAT1":0.1},{"date":"2014-05-1","CAT2":0.2},{"date":"2014-05-1","CAT3":0.3},{"date":"2014-05-1","UNSET":0.4},{"date":"2014-05-2","CAT1":0.4},{"date":"2014-05-2","CAT2":0.3},{"date":"2014-05-2","CAT3":0.2},{"date":"2014-05-2","UNSET":0.1}];

function groupProps(orig, key) {
    var newArr = [],
        groups = {},
        newItem, i, j, cur;
    for (i = 0, j = orig.length; i < j; i++) {
        cur = orig[i];
        if (!(cur[key] in groups)) {
            groups[cur[key]] = {date: cur[key] };
            newArr.push(groups[cur[key]]);
        }        
        for (var prop in cur) {
            if (prop != key) {
                groups[cur[key]][prop] = cur[prop];
            }
        }
    }
    return newArr;
}

console.log(groupProps(chartData, "date"))

答案 2 :(得分:1)

以下是一些代码,我们首先遍历数组,按日期将所有非日期属性组合在一起。然后将date属性附加到该中间结果:

var chartData = [
        {"date": "2014-05-1", "CAT1": 0.1},
        {"date": "2014-05-1", "CAT2": 0.2},
        {"date": "2014-05-1", "CAT3": 0.3},
        {"date": "2014-05-1", "UNSET": 0.4},
        {"date": "2014-05-2", "CAT1": 0.4},
        {"date": "2014-05-2", "CAT2": 0.3},
        {"date": "2014-05-2", "CAT3": 0.2},
        {"date": "2014-05-2", "UNSET": 0.1}
    ];


function mergeValues(chartData) {
    var tempObj = {};
    for (i in chartData) {
        var date = chartData[i].date;
//remove the date
        delete chartData[i].date;
//get the remaining keys
        var keys = Object.keys(chartData[i]);
        tempObj[date] = tempObj[date] || {};
        for (j in keys) {
            tempObj[date][keys[j]] = chartData[i][keys[j]];
        }

    }
    console.log(tempObj);
//{"2014-05-1":{ CAT1:0.1, CAT2:0.2, CAT3:0.3, UNSET:0.4}
//{"2014-05-2":{ CAT1:0.4, CAT2:0.3, CAT3:0.2, UNSET:0.1}



    var arr = [];
    var keys = Object.keys(tempObj);
    for (k in keys) {
        var obj = tempObj[keys[k]];
//add the date
        obj.date = keys[k];
        arr.push(obj);
    }

    return arr;

}
console.log(mergeValues(chartData));
//
//[
//    {"CAT1":0.1,"CAT2":0.2,"CAT3":0.3,"UNSET":0.4,"date":"2014-05-1"},           
//    {"CAT1":0.4,"CAT2":0.3,"CAT3":0.2,"UNSET":0.1,"date":"2014-05-1"}
//]

答案 3 :(得分:1)

使用underscore.js,可以很容易地完成:

var groupedBy = _.groupBy(chartData, 'date');
var a = _.reduce(groupedBy, function(a, c) {
    a.push(_.reduce(c, function(a2, c2){ 
        for(var i in c2) { a2[i] = c2[i]; }
        return a2;
    }, { }));
    return a;
},[]); // 'a' holds the desired merged object