第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

重新格式化 JS 對(duì)象以獲得年/月/周總計(jì)

重新格式化 JS 對(duì)象以獲得年/月/周總計(jì)

HUX布斯 2023-09-14 20:13:52
我有數(shù)據(jù)如下:{   "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"            }         ]      }   }}我需要按以下格式構(gòu)建最終數(shù)據(jù):[{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}]目的是獲得年/月/周總計(jì)。稍后繪制一些圖表需要這些數(shù)據(jù)。非常感謝對(duì)此的幫助...!...預(yù)先感謝 ASJ
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

試試這個(gè)(內(nèi)聯(lián)評(píng)論中有詳細(xì)信息):


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

我認(rèn)為你的輸出應(yīng)該是 5 個(gè)項(xiàng)目,而不是 4 個(gè)。對(duì)于現(xiàn)有的每周 1 個(gè)項(xiàng)目。輸出:


[

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

]


查看完整回答
反對(duì) 回復(fù) 2023-09-14
?
九州編程

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊

嘗試這個(gè)


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


查看完整回答
反對(duì) 回復(fù) 2023-09-14
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

我們可以通過循環(huán)每年、每月、每周來轉(zhuǎn)換數(shù)據(jù)。然后根據(jù)我們創(chuàng)建的信息構(gòu)造一個(gè)對(duì)象。


假設(shè)ogData是您要轉(zhuǎn)換的數(shù)據(jù)


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

            });

        }

    }

}

現(xiàn)在我們只需要將月份從數(shù)字轉(zhuǎn)換為名稱。


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

}

上面的代碼一起為您的數(shù)據(jù)集輸出以下內(nèi)容


[{"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}]


查看完整回答
反對(duì) 回復(fù) 2023-09-14
  • 3 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)