我正在尝试将从API
接收到的某些数据填充到图表中,并且为了使其“正常工作”,我需要确保数组中的每个对象都具有{{1} } 12
属性下的值(一年中的每个月一个值)。
我从sum
收到的数据如下:
API
因此,如您所见,[
{
"sum": {
"201901": 8,
"201902": 16,
"201903": 8,
"201904": 8,
/* This project is missing data for month 5 and 6 */
"201907": 5,
"201908": 2,
"201909": 0,
"201910": 2,
"201911": 5,
"201912": 8
},
"project": "Project Name 1"
},
{
"sum": {
"201901": 9,
"201902": 18,
"201903": 9,
"201904": 9,
"201905": 17,
"201906": 5,
"201907": 2,
"201908": 4,
/* This project is missing data for month 9, 10, 11, 12 */
},
"project": "Project Name 2"
},
{
"sum": {
"201901": 2,
"201902": 2,
"201903": 5,
"201904": 2,
"201905": 0,
"201906": 10,
"201907": 45,
"201908": 21,
"201909": 3,
"201910": 2,
"201911": 17,
"201912": 16
},
"project": "Project Name 3"
}
]
和project 1
缺失了几个月,但是我仍然需要用空值填充它们(例如:project 2
,{{ 1}}等),因为我的图表中的"201905": 0
始终是"201906": 0
值,如果X-axis
没有给出12
值,就会搞砸了。
所以我的问题是:如何填充每个对象以始终为一年中的每个月提供一个值?
到目前为止,我正在这样遍历数据:
Y-axis
有效地给了我一些帮助:
12
答案 0 :(得分:1)
您可以取一个空的sum对象并将值分配给一个新对象。
var data = [{ sum: { 201901: 8, 201902: 16, 201903: 8, 201904: 8, 201907: 5, 201908: 2, 201909: 0, 201910: 2, 201911: 5, 201912: 8 }, project: "Project Name 1" }, { sum: { 201901: 9, 201902: 18, 201903: 9, 201904: 9, 201905: 17, 201906: 5, 201907: 2, 201908: 4 }, project: "Project Name 2" }, { sum: { 201901: 2, 201902: 2, 201903: 5, 201904: 2, 201905: 0, 201906: 10, 201907: 45, 201908: 21, 201909: 3, 201910: 2, 201911: 17, 201912: 16 }, project: "Project Name 3" }], empty = { 201901: 0, 201902: 0, 201903: 0, 201904: 0, 201905: 0, 201906: 0, 201907: 0, 201908: 0, 201909: 0, 201910: 0, 201911: 0, 201912: 0 },
empty = { 201901: 0, 201902: 0, 201903: 0, 201904: 0, 201905: 0, 201906: 0, 201907: 0, 201908: 0, 201909: 0, 201910: 0, 201911: 0, 201912: 0 },
result = data.map(({ sum, ...rest }) => ({ sum: { ...empty, ...sum }, ...rest }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
如果您定义一个包含所有12 keys
的对象,例如:
const normalizedSum = {
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"201912": 0
};
然后,您可以像这样在input
数组上使用Array.map():
input.map(({sum, project}) => ({sum: {...normalizedSum, ...sum}, project}));
const input = [{"sum":{"201901":8,"201902":16,"201903":8,"201904":8,"201907":5,"201908":2,"201909":0,"201910":2,"201911":5,"201912":8},"project":"Project Name 1"},{"sum":{"201901":9,"201902":18,"201903":9,"201904":9,"201905":17,"201906":5,"201907":2,"201908":4,},"project":"Project Name 2"},{"sum":{"201901":2,"201902":2,"201903":5,"201904":2,"201905":0,"201906":10,"201907":45,"201908":21,"201909":3,"201910":2,"201911":17,"201912":16},"project":"Project Name 3"}];
const normalizedSum = {
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"201912": 0
};
let res = input.map(
({sum, project}) => ({sum: {...normalizedSum, ...sum}, project})
);
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 2 :(得分:1)
您可以做的是制作一个模板对象,该对象具有全部12个月的默认值0
。然后浏览sum
项,如果它们具有匹配的日期键,则将其设置为sum
中键的值:
for (let key in chartData) {
// template object holding 0s
let data = { "201901": 0, "201902": 0, "201903": 0, "201904": 0, "201905": 0, "201906": 0, "201907": 0, "201908": 0, "201909": 0, "201910": 0, "201911": 0, "201912": 0 };
// For each key that exist in the sum object populate data with that value
for (let month in chartData[key].sum) data[month] = chartData[key].sum[month];
emptyArr.push({
name: chartData[key].project,
data: Object.values(data), // Now you can do the same thing you where doing before
});
}
这是一个完整的例子:
let chartData = [
{
"sum": {
"201901": 8,
"201902": 16,
"201903": 8,
"201904": 8,
/* This project is missing data for month 5 and 6 */
"201907": 5,
"201908": 2,
"201909": 0,
"201910": 2,
"201911": 5,
"201912": 8
},
"project": "Project Name 1"
},
{
"sum": {
"201901": 9,
"201902": 18,
"201903": 9,
"201904": 9,
"201905": 17,
"201906": 5,
"201907": 2,
"201908": 4,
/* This project is missing data for month 9, 10, 11, 12 */
},
"project": "Project Name 2"
},
{
"sum": {
"201901": 2,
"201902": 2,
"201903": 5,
"201904": 2,
"201905": 0,
"201906": 10,
"201907": 45,
"201908": 21,
"201909": 3,
"201910": 2,
"201911": 17,
"201912": 16
},
"project": "Project Name 3"
}
];
let emptyArr = []
for (let key in chartData) {
let data = { "201901": 0, "201902": 0, "201903": 0, "201904": 0, "201905": 0, "201906": 0, "201907": 0, "201908": 0, "201909": 0, "201910": 0, "201911": 0, "201912": 0 };
for (let month in chartData[key].sum) data[month] = chartData[key].sum[month];
emptyArr.push({
name: chartData[key].project,
data: Object.values(data),
});
}
console.log(emptyArr);