简体   繁体   English

如何为angular / typescript应用显示和过滤此数组?

[英]How to display and filter this array for angular/typescript app?

I'm wanting to group and display the data by BreakdownPalletID in the array below. 我想在下面的数组中按BreakdownPalletID分组并显示数据。 I'm using typescript/angular and wanting to show this data grouped by Breakdown Pallet ID and then for each show the list of LPs and Skus for each one but i'm not sure how to do that with the JSON object the way it is below. 我正在使用Typescript / Angular并想显示此数据,并按Breakdown Pallet ID分组,然后为每个显示每个LP和Skus的列表,但是我不确定如何使用JSON对象进行处理下面。 The number of breakdownPalletIDs is also dynamic BreakingPalletID的数量也是动态的

 {
        "palletCount": 4,
        "lpCount": 6,
        "Sku": [
            {
                "sku": 394211,
                "lp": "00006844763464291206",
                "breakdownPalletID": 4,
                "customerMessages": ["DO NOT STACK", "DO NOT DO THAT"]

            },
            {
                "sku": 395299,
                "lp": "00006844763464289555",
                "breakdownPalletID": 1,
                "customerMessages": ["MAKE IT 3 HIGH AND LOW"]
            },
            {
                "sku": 460957,          
                "lp": "00006844763464289555",
                "breakdownPalletID": 4,
                    "customerMessages": ["DO NOT STACK", "DO NOT DO THAT"]
            },
            {
                "sku": 460957,      
                "lp": "00006844763464291039",
                "breakdownPalletID": 2,
                "customerMessages": ["MESSAGE 1","MESSAGE 2","MESSAGE 3"]
            },
            {
                "sku": 461716,      
                "lp": "00006844763464291336",
                "breakdownPalletID": 1,
                "customerMessages": ["MAKE IT 3 HIGH AND LOW"]
            },
            {
                "sku": 506733,          
                "lp": "00006844763464289555",
                "breakdownPalletID": 1,
            "customerMessages": ["MAKE IT 3 HIGH AND LOW"]
            },
            {
                "sku": 519795,          
                "lp": "00006844763464291206",
                "breakdownPalletID": 3,
                "customerMessages": ["MESSAGE 5","MESSAGE 6","MESSAGE 7"]

            }
            ]
    } 

You can do something like this, without importing any other library: 您可以执行以下操作,而无需导入任何其他库:

 // Build an array of the Pallet IDs
    this.pallets = this.source.Sku.map(item => {
        return item.breakdownPalletID;
    }).filter((item, index, self) => {// Remove duplicate Pallet IDs
        return index === self.indexOf(item);
    });

    // Sort Pallet IDs (optional)
    this.pallets.sort();

    // Create a new array with the items grouped by PalletId
    this.pallets.forEach(pId => {
        this.target.push({
            PalletId: pId,
            Items: this.source.Sku.filter(item => item.breakdownPalletID === pId)
        });
    });

Where, "this.source" is your original object. 其中,“ this.source”是您的原始对象。 The output is an array of object you can use with an *ngFor. 输出是可以与* ngFor一起使用的对象数组。

See demo 观看演示

You can try something like this. 您可以尝试这样。

  groupBy(origArray: any[], key: string) {
    return origArray.reduce(function (rv, x) {
      (rv[x[key]] = rv[x[key]] || []).push(x);
      return rv;
    }, {});
  }

See demo 观看演示

If you want to simplify life with libraries like lodash or underscore! 如果您想使用lodash或下划线之类的库来简化生活!

example with underscore: 带下划线的示例:

_.groupBy(this.data.Sku, (item: any) => item.breakdownPalletID);

will give you result like this: 会给你这样的结果:

    {
  "1": [
    {
      "sku": 395299,
      "lp": "00006844763464289555",
      "breakdownPalletID": 1
    },
    {
      "sku": 461716,
      "lp": "00006844763464291336",
      "breakdownPalletID": 1
    },
    {
      "sku": 506733,
      "lp": "00006844763464289555",
      "breakdownPalletID": 1
    }
  ],
  "2": [
    {
      "sku": 460957,
      "lp": "00006844763464291039",
      "breakdownPalletID": 2
    }
  ],
  "3": [
    {
      "sku": 519795,
      "lp": "00006844763464291206",
      "breakdownPalletID": 3
    }
  ],
  "4": [
    {
      "sku": 394211,
      "lp": "00006844763464291206",
      "breakdownPalletID": 4
    },
    {
      "sku": 460957,
      "lp": "00006844763464289555",
      "breakdownPalletID": 4
    }
  ]
}

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

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