简体   繁体   English

如何将项目推送和分组到数组

[英]How to push and group item to Array

I have searched many sites to find the answer to this question, I do not know exactly how to describe it, so, excuse me.我搜索了很多网站来找到这个问题的答案,我不知道如何确切地描述它,所以,请原谅。 I have a .json file with this structure:我有一个具有以下结构的 .json 文件:

"ntest": [
    {
        "dane": [
            {
                "label": "Product A",
                "mydane": [
                    {
                        "a": 20,
                        "y": "2018"
                    },
                    {
                        "a": 15,
                        "y": "2019"
                    },
                    {
                        "a": 35,
                        "y": "2020"
                    }
                ]
            },
            {
                "label": "Product B",
                "mydane": [
                    {
                        "a": 10,
                        "y": "2018"
                    },
                    {
                        "a": 15,
                        "y": "2019"
                    },
                    {
                        "a": 28,
                        "y": "2020"
                    }
                ]
            }
        ]

and what I want is to receive something like this:我想要的是收到这样的东西:

在此处输入图片说明

apart I want to have only one value of each year.除了我想每年只有一个值。 I have already reached this point but I cannot access the data, and the result is as follow.我已经到了这一点,但我无法访问数据,结果如下。

在此处输入图片说明

And this is my method.这是我的方法。

getNewData(): void {
const ay: string[] = ['2018', '2019', '2020'];
const year = new Array();
this.getndata().subscribe((ko: any[]) => {
  ko.forEach((lp) => {
    lp.dane.forEach((el: { label: any; mydane: MyDane[]; }) => {
      console.log(el.label);
      console.log(el.mydane);
      for (const ite of el.mydane) {
        year.push(ite.y);
      }
    });
  });
});
console.log(JSON.stringify(ay));
console.log(JSON.stringify(year));

// this.barChartLabels = yearray;

} }

How can I get the result as in the first image?如何获得第一张图像中的结果? This is sringifi result这是 sringifi 结果

在此处输入图片说明

You can use a dictionary, also you can remove duplicates from the array, as Kelvin's said, then sorting them (dictionaries do that auto)您可以使用字典,也可以从数组中删除重复项,正如开尔文所说,然后对它们进行排序(字典自动执行)

 let data = { "ntest": [ { "dane": [ { "label": "Product A", "mydane": [ { "a": 20, "y": "2018" }, { "a": 15, "y": "2019" }, { "a": 35, "y": "2020" } ] }, { "label": "Product B", "mydane": [ { "a": 10, "y": "2018" }, { "a": 15, "y": "2019" }, { "a": 28, "y": "2020" } ] } ] } ] } let yearsDict = {} data.ntest[0].dane.forEach((dane) => { dane.mydane.forEach((obj) => { yearsDict[obj.y] = obj.a }) }) console.log(Object.getOwnPropertyNames(yearsDict))

For your problem, there are two easy solutions I can think of:对于您的问题,我能想到两个简单的解决方案:

  • Instead of getting all the y 's from every dane , only get them for the first one.不要从每个dane那里得到所有的y ,只为第一个得到它们。 That's assuming that the structure will be the same for all the dane s那是假设所有dane的结构都相同
  • Keep the result you have, but deduplicate the results.保留您拥有的结果,但对结果进行重复数据删除。 In other words, remove duplicates entries from the array换句话说,从数组中删除重复项

The latter can be easily done by storing your results in a Set instead of an array, which you can later convert to an array with Array.from(set) .后者可以通过将结果存储在Set而不是数组中来轻松完成,稍后您可以使用Array.from(set)将其转换为数组。 Sets don't store duplicates because of their nature.由于其性质,集合不存储重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM