将JSON数据推送到数组中。

huangapple go评论70阅读模式
英文:

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);

示例 @ StackBlitz

输出

将JSON数据推送到数组中。

英文:

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);

Demo @ StackBlitz

> Output

将JSON数据推送到数组中。

答案2

得分: 0

您正在尝试创建一个chart.js条形图的数据,其中包含两个数据集,分别对应于对象数组中的value1value2键的值。

以下是如何实现的:

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 = [{
    &quot;value1&quot;: 8,
    &quot;value2&quot;: 9,
    &quot;weekday&quot;: &quot;2023-02-27&quot;
  }, {
    &quot;value1&quot;: 8,
    &quot;value2&quot;: 9,
    &quot;weekday&quot;: &quot;2023-02-28&quot;
  }, {
    &quot;value1&quot;: 8,
    &quot;value2&quot;: 9,
    &quot;weekday&quot;: &quot;2023-03-01&quot;
  }, {
    &quot;value1&quot;: 8,
    &quot;value2&quot;: 5,
    &quot;weekday&quot;: &quot;2023-03-02&quot;
  }, {
    &quot;value1&quot;: 8,
    &quot;value2&quot;: 9,
    &quot;weekday&quot;: &quot;2023-03-03&quot;
  }
];
const weekdays = [&#39;Sunday&#39;, &#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;,
                  &#39;Friday&#39;, &#39;Saturday&#39;];

function getDayofWeek(date: any) {
  const dayOfWeek = new Date(date).getDay();
  return isNaN(dayOfWeek) ? null : weekdays[dayOfWeek];
}

const config = {
  type: &#39;bar&#39;,
  labels: data.map(({weekday}) =&gt; getDayofWeek(weekday)),
  datasets: [&#39;value1&#39;, &#39;value2&#39;].map((k) =&gt; ({
    data: data.map((v) =&gt; v[k as keyof typeof data[number]])
  }))
};

console.log(config);

<sup>Playground link</sup>

huangapple
  • 本文由 发表于 2023年3月1日 16:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定