猿问

重新格式化 JS 对象以获得年/月/周总计

我有数据如下:


{

   "2011":{

      "01":{

         "01":[

            {

               "date":"2011-01-01"

            },

            {

               "date":"2011-01-02"

            }

         ]

      },

      "02":{

         "01":[

            {

              

               "date":"2011-02-02"

            }

         ],

         "03":[

            {

               "date":"2011-02-15"

            },

            {                  

               "date":"2011-02-17"

            }

         ]

      }

   },

   "2012":{

      "01":{

         "01":[

            {

               "date":"2012-01-01"

            }

         ]

      },

      "03":{

         "01":[

            {

               "date":"2012-03-03"

            }

         ]

      }

   }

}

我需要按以下格式构建最终数据:


[{year:2011,month:'Jan',week_no:1, week_total:2},

{year:2011,month:'Jan',week_no:2, week_total:2},

{year:2012,month:'Jan',week_no:1, week_total:1}

{year:2012,month:'Mar',week_no:1, week_total:1}]

目的是获得年/月/周总计。稍后绘制一些图表需要这些数据。非常感谢对此的帮助...!...


预先感谢 ASJ


HUX布斯
浏览 96回答 3
3回答

Smart猫小萌

试试这个(内联评论中有详细信息):// Your dataconst data = {   "2011":{      "01":{         "01":[            {               "date":"2011-01-01"            },            {               "date":"2011-01-02"            }         ]      },      "02":{         "01":[            {                             "date":"2011-02-02"            }         ],         "03":[            {               "date":"2011-02-15"            },            {                                 "date":"2011-02-17"            }         ]      }   },   "2012":{      "01":{         "01":[            {               "date":"2012-01-01"            }         ]      },      "03":{         "01":[            {               "date":"2012-03-03"            }         ]      }   }};// Helper for month codesconst monthCodes = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];// Init empty plot data objectlet plotData = [];// Loop year input data objectfor (const [yearKey, yearValue] of Object.entries(data)) {  // Loop month input data object  for (const [monthKey, monthValue] of Object.entries(yearValue)) {    const weeksInMonth = Object.entries(monthValue);        // Loopp week input data    for (const [weekKey, weekValue] of weeksInMonth) {        // Create new data item for year and month        const dataItem = {             year: parseInt(yearKey),            month: monthCodes[parseInt(monthKey) - 1],            week_no: parseInt(weekKey),            week_total: weekValue.length        };              // Insert to plot data item        plotData.push(dataItem);    }  }}console.log(plotData);我认为你的输出应该是 5 个项目,而不是 4 个。对于现有的每周 1 个项目。输出:[    { month: "Jan", week_no: 1, week_total: 2, year: 2011 },    { month: "Feb", week_no: 1, week_total: 1, year: 2011 },    { month: "Feb", week_no: 3, week_total: 2, year: 2011 },    { month: "Jan", week_no: 1, week_total: 1, year: 2012 },    { month: "Mar", week_no: 1, week_total: 1, year: 2012 }]

九州编程

尝试这个var obj = JSON.parse(`{    "2011": {        "01": {            "01": [{                    "date": "2011-01-01"                },                {                    "date": "2011-01-02"                }            ]        },        "02": {            "01": [{                "date": "2011-02-02"            }],            "03": [{                    "date": "2011-02-15"                },                {                    "date": "2011-02-17"                }            ]        }    },    "2012": {        "01": {            "01": [{                "date": "2012-01-01"            }]        },        "03": {            "01": [{                "date": "2012-03-03"            }]        }    }}`);var output = [];    var months =  ["Jan","Feb","Mar","Apr","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];    for (var key in obj) {        for(var _k in obj[key]){            var _int = obj[key][_k];            for(var _w in _int){                output.push({year: parseInt(key), month: months[parseInt(_k)-1], week_no: parseInt(_w), week_total: _int[_w].length});            }        }    }    console.log(output);

慕慕森

我们可以通过循环每年、每月、每周来转换数据。然后根据我们创建的信息构造一个对象。假设ogData是您要转换的数据var years = Object.keys(ogData);var newData = [];for (var i=0; i<years.length; i++) {console.log("1")&nbsp; &nbsp; var year = years[i];&nbsp; &nbsp; var months = Object.keys(ogData[year]);&nbsp; &nbsp; for (var i2=0; i2<months.length; i2++) {console.log("2")&nbsp; &nbsp; &nbsp; &nbsp; var month = months[i2];&nbsp; &nbsp; &nbsp; &nbsp; var weeks = Object.keys(ogData[year][month]);&nbsp; &nbsp; &nbsp; &nbsp; for (var i3=0; i3<weeks.length; i3++) {console.log("3")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var week = weeks[i3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newData.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; year: year,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month: toName(month),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; week_no: week,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; week_total: ogData[year][month][week].length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}现在我们只需要将月份从数字转换为名称。var all_months = [&nbsp; &nbsp; "Jan",&nbsp; &nbsp; "Feb",&nbsp; &nbsp; "Mar",&nbsp; &nbsp; "Apr",&nbsp; &nbsp; "Jun",&nbsp; &nbsp; "Jul",&nbsp; &nbsp; "Aug",&nbsp; &nbsp; "Sep",&nbsp; &nbsp; "Oct",&nbsp; &nbsp; "Nov",&nbsp; &nbsp; "Dec"]function toName(num) {&nbsp; &nbsp; // `parseInt` accepts `"01"` as `1`&nbsp; &nbsp; num = parseInt(num);&nbsp; &nbsp; // `-1` to start index at 0&nbsp; &nbsp; return all_months[num-1];}上面的代码一起为您的数据集输出以下内容[{"year":"2011","month":"Jan","week_no":"01","week_total":2},{"year":"2011","month":"Feb","week_no":"01","week_total":1},{"year":"2011","month":"Feb","week_no":"03","week_total":2},{"year":"2012","month":"Jan","week_no":"01","week_total":1},{"year":"2012","month":"Mar","week_no":"01","week_total":1}]
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答