https://www.echojs.com/news/38077
const input = {
"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"
}
]
}
}
}
const monthNames = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
]
function format(data) {
const output = [];
Object.entries(data).forEach(([yearString, monthData]) => {
const year = parseInt(yearString, 10)
Object.entries(monthData).forEach(([monthString, weekData]) => {
const month = monthNames[parseInt(monthString, 10) - 1]
const weekKeys = Object.keys(weekData)
const week_total = weekKeys.length
weekKeys.forEach((weekString) => {
const week_no = parseInt(weekString, 10)
output.push({ year, month, week_no, week_total })
})
})
})
return output
}
format(input)
no comments