简体   繁体   English

在 javascript 中合并具有相同索引的 json 列表

[英]Merge json list having same index in javascript

I have the following json data:我有以下 json 数据:

{
    "leadId": 0,

    "nationalAvg": {
        "2007": 822.0,
        "2008": 830.0,
        "2009": 880.0,
        "2010": 909.0,
        "2011": 979.0,            
    },
    "stateAvg": {
        "2007": 1023.0,
        "2008": 1026.0,
        "2009": 1035.0,
        "2010": 1050.0,
        "2011": 1072.0,
        "2012": 1150.0,
    },
    "zipCodeAvg": {
        "2007": 1050.98,
        "2008": 1054.06,
        "2009": 1063.29,
        "2010": 1078.69,
        "2011": 1101.27,
        "2012": 1181.49,
        "2013": 1297.64,
        "2014": 1349.99,
    }
}

I want to merge nationalAvg, stateAvg, zipCodeAvg together to have a result like:我想将nationalAvg、stateAvg、zipCodeAvg 合并在一起,得到如下结果:

["2007":  822.0,      1023.0,   1050.98]
["2008":  830.0,      1026.0,   1054.06]
["2009":  880.0,      1035.0,   1063.29]
["2010":  909.0,      1050.0,   1078.69]
["2011":  null,       1072.0,   1101.27]
["2012":  null,      1150.0,    1181.49]
["2013":  null,      null,      1297.64]
["2014":  null,      null,      1349.99]

I want to show this data on google line chart.我想在谷歌折线图上显示这些数据。

You can do something like this:你可以这样做:

 const data = { "leadId": 0, "nationalAvg": { "2007": 822.0, "2008": 830.0, "2009": 880.0, "2010": 909.0, "2011": 979.0, }, "stateAvg": { "2007": 1023.0, "2008": 1026.0, "2009": 1035.0, "2010": 1050.0, "2011": 1072.0, "2012": 1150.0, }, "zipCodeAvg": { "2007": 1050.98, "2008": 1054.06, "2009": 1063.29, "2010": 1078.69, "2011": 1101.27, "2012": 1181.49, "2013": 1297.64, "2014": 1349.99, } } const keys = ['nationalAvg', 'stateAvg', 'zipCodeAvg']; const years = keys.reduce((a, k) => [...a, ...Object.keys(data[k])], []).filter((v, i, arr) => arr.indexOf(v) === i).sort(); const result = years.map((year) => [year, ...keys.reduce((a, k) => [...a, data[k][year]], [])] ); console.log(result);

How this works: it first creates an array of all the years (all the keys of the three objects), filters out all the duplicates from that array and then map it to add the value for those years (those properties) from the three objects.这是如何工作的:它首先创建一个包含所有年份的数组(三个对象的所有键),从该数组中过滤掉所有重复项,然后 map 它从三个对象中添加这些年份的值(那些属性) .

 const list = { "leadId": 0, "nationalAvg": { "2007": 822.0, "2008": 830.0, "2009": 880.0, "2010": 909.0, "2011": 979.0, }, "stateAvg": { "2007": 1023.0, "2008": 1026.0, "2009": 1035.0, "2010": 1050.0, "2011": 1072.0, "2012": 1150.0, }, "zipCodeAvg": { "2007": 1050.98, "2008": 1054.06, "2009": 1063.29, "2010": 1078.69, "2011": 1101.27, "2012": 1181.49, "2013": 1297.64, "2014": 1349.99, } } let keys = ['nationalAvg', 'stateAvg', 'zipCodeAvg']; let obj =keys.reduce((acc, c, i) => { Object.entries(list[c]).map(( o ) => { acc[o[0]] = (acc[o[0]] || Array.from({length: keys.length}).fill(null)); acc[o[0]][i] = (o[1]); }); return acc; }, {}); // Format 1 console.log(obj); const result = Object.entries(obj).map(o=> [ o[0], ...o[1]]); //Format 2 console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM