3 回答
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 }
]
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);
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}]
添加回答
举报