Javascript 重新格式化JS对象以获取年/月/周总数
我有以下资料:Javascript 重新格式化JS对象以获取年/月/周总数,javascript,Javascript,我有以下资料: { "2011":{ "01":{ "01":[ { "date":"2011-01-01" }, { "date":"2011-01-02" }
{
"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我们可以通过每年、每月和每周循环来转换数据。 然后根据我们创建的信息构造一个对象 假设
ogData
是您希望转换的数据
var years=Object.key(ogData);
var newData=[];
对于(var i=0;i请尝试此操作(内联注释中的详细信息):
我认为你的输出应该是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": [{
“日期”:“2011-01-01”
},
{
“日期”:“2011-01-02”
}
]
},
"02": {
"01": [{
“日期”:“2011-02-02”
}],
"03": [{
“日期”:“2011-02-15”
},
{
“日期”:“2011-02-17”
}
]
}
},
"2012": {
"01": {
"01": [{
“日期”:“2012-01-01”
}]
},
"03": {
"01": [{
“日期”:“2012-03-03”
}]
}
}
}`);
var输出=[];
风险值月份=[“一月”、“二月”、“三月”、“四月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
for(obj中的var键){
for(对象[key]中的变量k){
var[u int=obj[key][[u k];
用于(变量w in_int){
push({年:parseInt(键),月:月[parseInt(_k)-1],周号:parseInt(_w),周总数:_int[_w].length});
}
}
}
console.log(output);
这一个绝对是最正确和最可读的。我考虑过使用Array.prototype.flatMap()
,但似乎使用嵌套for循环更有效。
// 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);
[
{ 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 }
]