简体   繁体   中英

Remove item from the list dynamically if there are no child elements

I have 3 array lists as below:

 groups = [ { grpId: 1, grpName: "Vegetables" }, { grpId: 2, grpName: "Fruits" }, { grpId: 3, grpName: "Staples" }];
 subGrp = [ { subGrpId: "V1", grpId: 1, subGrpName: "Carrot" }, { subGrpId: "F1", grpId: 2, subGrpName: "Apple" },
            { subGrpId: "S1", grpId: 3, subGrpName: "Dal" }];
 quantity = [ { quantityValue: "1kg", subGrpId: "V1" }, { quantityValue: "1kg", subGrpId: "S1" }];

Condition: If there aren't any "quantity" related to "subGrp" Array, then we need to remove the whole entry from the custom list.

Using above array lists, I am trying to build a custom list as below:

const grpSet = new Set(this.groups.map(i => ({ id: i.grpId, name: i.grpName }))); 
grpSet.forEach(g =>
  this.groceryList.push({
    grp: g["name"],
    grpTypeList: this.subGrp.filter(el => {
      if (el.grpId === g["id"] && this.quantity) {
        el["quantities"] = this.quantity.filter(
          v => v.subGrpId === el["subGrpId"]
        );
        if (el["quantities"].length > 0) {
          return el;
        }
      }
    })
  })
);

// above logic will lead us to below custom list. 

From this, I need to remove Fruits entry since grpTypeList is empty. But my logic is not allowing me to do it.

[{
    "grp": "Vegetables",
    "grpTypeList": [{
        "subGrpId": "V1",
        "grpId": 1,
        "subGrpName": "Carrot",
        "quantities": [{
                "quantityValue": "1kg",
                "subGrpId": "V1"
            },
            {
                "quantityValue": "2kg",
                "subGrpId": "V1"
            }
        ]
    }]
},
// remove this complete object as grpTypeList is empty
{
    "grp": "Fruits",
    "grpTypeList": [

    ]
}, 
{
    "grp": "Staples",
    "grpTypeList": [{
        "subGrpId": "S1",
        "grpId": 3,
        "subGrpName": "Dal",
        "quantities": [{
            "quantityValue": "1kg",
            "subGrpId": "S1"
        }]
    }]
} ]

Using above list, I am plotting my html to display as below figure:

在此处输入图像描述

there are just minor changes that you need to take care of, Please update your code with the below lines:

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  groceryList = [];
  groups = [
    { grpId: 1, grpName: "Vegetables" },
    { grpId: 2, grpName: "Fruits" },
    { grpId: 3, grpName: "Staples" }
  ];
  subGrp = [
    { subGrpId: "V1", grpId: 1, subGrpName: "Carrot" },
    { subGrpId: "F1", grpId: 2, subGrpName: "Apple" },
    { subGrpId: "S1", grpId: 3, subGrpName: "Dal" }
  ];
  quantity = [
    { quantityValue: "1kg", subGrpId: "V1" },
    { quantityValue: "2kg", subGrpId: "V1" },
    { quantityValue: "1kg", subGrpId: "S1" }
  ];

  ngOnInit() {
    this.groceryList = [];
    const grpSet = new Set(
      this.groups.map(i => ({ id: i.grpId, name: i.grpName }))
    );

    grpSet.forEach(g =>{
      let element:any[];
      element= this.subGrp.filter(el => {
          if (el.grpId === g["id"] && this.quantity) {
            el["quantities"] = this.quantity.filter(
              v => v.subGrpId === el["subGrpId"]
            );
            if (el["quantities"].length > 0) {
              return el;
            }
          }
        })
        if(element && element.length){
        this.groceryList.push({
          grp:g["name"],
          grpTypeList:element
        })
        }
    }
    );
    console.log(JSON.stringify(this.groceryList));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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