简体   繁体   中英

Merge json list having same index in javascript

I have the following json 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,
    }
}

I want to merge nationalAvg, stateAvg, zipCodeAvg together to have a result like:

["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.

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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