亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重新格式化 JS 對象以獲得年/月/周總計

重新格式化 JS 對象以獲得年/月/周總計

HUX布斯 2023-09-14 20:13:52
我有數據如下:{   "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
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

試試這個(內聯評論中有詳細信息):


// Your data

const 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 codes

const monthCodes = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 

  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];


// Init empty plot data object

let plotData = [];


// Loop year input data object

for (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 }

]


查看完整回答
反對 回復 2023-09-14
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

嘗試這個


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);


查看完整回答
反對 回復 2023-09-14
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

我們可以通過循環每年、每月、每周來轉換數據。然后根據我們創建的信息構造一個對象。


假設ogData是您要轉換的數據


var years = Object.keys(ogData);

var newData = [];


for (var i=0; i<years.length; i++) {console.log("1")

    var year = years[i];

    var months = Object.keys(ogData[year]);

    for (var i2=0; i2<months.length; i2++) {console.log("2")

        var month = months[i2];

        var weeks = Object.keys(ogData[year][month]);

        for (var i3=0; i3<weeks.length; i3++) {console.log("3")

            var week = weeks[i3];

            newData.push({

                year: year,

                month: toName(month),

                week_no: week,

                week_total: ogData[year][month][week].length

            });

        }

    }

}

現在我們只需要將月份從數字轉換為名稱。


var all_months = [

    "Jan",

    "Feb",

    "Mar",

    "Apr",

    "Jun",

    "Jul",

    "Aug",

    "Sep",

    "Oct",

    "Nov",

    "Dec"

]

function toName(num) {

    // `parseInt` accepts `"01"` as `1`

    num = parseInt(num);

    // `-1` to start index at 0

    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}]


查看完整回答
反對 回復 2023-09-14
  • 3 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號