英文:
Angular Push JSON data into Array
问题
我试图弄清楚如何在 TypeScript 中将 JSON 对象推送到数组中。我已经成功地用 JSON 对象的一个值来实现这一点,但我也需要用第二个值来实现。
这是 JSON 文件的结构:
data = [
{
"value1": 8,
"value2": 9,
"weekday": "2023-02-27"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-02-28"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-03-01"
},
{
"value1": 8,
"value2": 5,
"weekday": "2023-03-02"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-03-03"
}
];
这是我的代码:
setData() {
let data_value1: any[] = [];
let data_value2: any[] = [];
let labels: any[] = [];
this.data.forEach((entry) => {
data_value1.push(entry['value1']);
data_value2.push(entry['value2']);
labels.push(getDayofWeek(entry['weekday']));
});
this.barChartData.labels = labels;
let dataSet: any = {};
dataSet['data'] += data_value1;
dataSet['data'] += data_value2;
dataSet['label'] = 'Test';
this.barChartData.datasets.push(dataSet);
function getDayofWeek(date: any) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null :
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][dayOfWeek];
}
}
到目前为止,如果我只将一个对象值推送到数据集数组中,它可以工作,但如果我尝试使用 +=
来推送它,它不起作用。我做错了什么?
我尝试了多种方法,但似乎什么都不起作用。我无法理解这个问题。提前感谢!
英文:
im trying to figure out how to push a json object into an array using typescript. I managed to do it with one value of the json object but i need to do it with the second one too.
Heres the Structure of the JSON file:
data = [
{
"value1": 8,
"value2": 9,
"weekday": "2023-02-27"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-02-28"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-03-01"
},
{
"value1": 8,
"value2": 5,
"weekday": "2023-03-02"
},
{
"value1": 8,
"value2": 9,
"weekday": "2023-03-03"
}
];
And heres my code:
setData() {
let data_value1:any[] = [];
let data_value2:any[] = [];
let labels:any[] = [];
this.data.forEach((entry) => {
data_value1.push(entry['value1']);
data_value2.push(entry['value2']);
labels.push(getDayofWeek(entry['weekday']));
});
this.barChartData.labels = labels;
let dataSet:any = {};
dataSet['data'] += data_value1;
dataSet['data'] += data_value2;
dataSet['label'] = 'Test';
this.barChartData.datasets.push(dataSet);
function getDayofWeek(date:any) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null :
['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][dayOfWeek];
}
}
It works so far if i only push one object value into the dataset array, but if i try it with += it doesnt work. What am i doing wrong?
I tried multiple stuff but nothing seems to work. I just can't wrap my head around it.
Thanks in advance!
答案1
得分: 0
从你的代码来看,我相信你试图创建一个具有多个数据集的条形图。
你应该有两个不同的数据集:
[
{
"data": [ 8, 8, 8, 8, 8 ],
"label": "标签 1"
},
{
"data": [ 9, 9, 9, 5, 9 ],
"label": "标签 2"
}
]
let dataSet1: any = {
data: data_value1,
label: '标签 1',
};
let dataSet2: any = {
data: data_value2,
label: '标签 2',
};
this.barChartData.datasets.push(dataSet1);
this.barChartData.datasets.push(dataSet2);
输出
英文:
From your code, I believe that you try to achieve a bar chart with multiple datasets.
You should have 2 different datasets:
[
{
"data": [ 8, 8, 8, 8, 8 ],
"label": "Label 1"
},
{
"data": [ 9, 9, 9, 5, 9 ],
"label": "Label 2"
}
]
let dataSet1: any = {
data: data_value1,
label: 'Label 1',
};
let dataSet2: any = {
data: data_value2,
label: 'Label 2',
};
this.barChartData.datasets.push(dataSet1);
this.barChartData.datasets.push(dataSet2);
> Output
答案2
得分: 0
您正在尝试创建一个chart.js条形图的数据,其中包含两个数据集,分别对应于对象数组中的value1
和value2
键的值。
以下是如何实现的:
const data = [{
"value1": 8,
"value2": 9,
"weekday": "2023-02-27"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-02-28"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-03-01"
}, {
"value1": 8,
"value2": 5,
"weekday": "2023-03-02"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-03-03"
}
];
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'];
function getDayofWeek(date: any) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null : weekdays[dayOfWeek];
}
const config = {
type: 'bar',
labels: data.map(({weekday}) => getDayofWeek(weekday)),
datasets: ['value1', 'value2'].map((k) => ({
data: data.map((v) => v[k as keyof typeof data[number]])
}))
};
console.log(config);
<sup>Playground link</sup>
英文:
You're trying to create data for a chart.js bar chart with two datasets corresponding to the values for the value1
and value2
keys in your object array.
Here's how to do it:
const data = [{
"value1": 8,
"value2": 9,
"weekday": "2023-02-27"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-02-28"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-03-01"
}, {
"value1": 8,
"value2": 5,
"weekday": "2023-03-02"
}, {
"value1": 8,
"value2": 9,
"weekday": "2023-03-03"
}
];
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday'];
function getDayofWeek(date: any) {
const dayOfWeek = new Date(date).getDay();
return isNaN(dayOfWeek) ? null : weekdays[dayOfWeek];
}
const config = {
type: 'bar',
labels: data.map(({weekday}) => getDayofWeek(weekday)),
datasets: ['value1', 'value2'].map((k) => ({
data: data.map((v) => v[k as keyof typeof data[number]])
}))
};
console.log(config);
<sup>Playground link</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论