簡體   English   中英

在角度java腳本中過濾嵌套的對象數組

[英]Filter Nested array of objects in angular java script

嗨,我有以下數組。 我想根據用戶輸入過濾這個數組。我在互聯網上搜索它,通過了許多關於堆棧溢出的解決方案,我也瀏覽了數組的文檔,但我沒有得到任何解決方案。 請幫忙...

{
  "data": [
    {
      "categoryId": "1",
      "categoryName": "Pens",
      "subcat": [
        {
          "SubCatId": "1",
          "SubCategoryName": "Classic Cakes",
          "item": [
            {
              "DisplayName": "Excellent 500gms",
              "ItemData": {
                "ItemName": "Excellent"
              }
            },
            {
              "DisplayName": "choco vanila  500gms",
              "ItemData": {
                "Id": "26",
                "ItemName": "choco vanila "
              }
            }
          ]
        },
                {
          "SubCatId": "2",
          "SubCategoryName": "Classic Cakes2",
          "item": [
            {
              "DisplayName": "xyz 500gms",
              "ItemData": {
                "ItemName": "xyz"
              }
            },
            {
              "DisplayName": "abc  500gms",
              "ItemData": {
                "Id": "26",
                "ItemName": "abc vanila "
              }
            }
          ]
        }
      ]
    },
    {
      "categoryId": "2",
      "categoryName": "Markers",
      "subcat": [
        {
          "SubCatId": "2",
          "SubCategoryName": "Premium Cakes I",
          "item": [
            {
              "DisplayName": "choco caramel 500gms",
              "ItemData": {
                "Id": "65",
                "ItemName": "choco caramel"
              }
            },
            {
              "DisplayName": "choco  almond  500gms",
              "ItemData": {
                "Id": "52",
                "ItemName": "choco  almond "
              }
            }
  
          ]
        }
      ]
    }
 
  ]
}

我想對 'categoryName' 、 'SubCategoryName' 、 'DisplayName' 、 'ItemName' 應用過濾器

例如,如果我搜索“巧克力杏仁”(即“ItemName”),那么結果數組應該類似於

{
  "data": [
    {
      "categoryId": "2",
      "categoryName": "Markers",
      "subcat": [
        {
          "SubCatId": "2",
          "SubCategoryName": "Premium Cakes I",
          "item": [
            {
              "DisplayName": "choco  almond  500gms",
              "ItemData": {
                "Id": "52",
                "ItemName": "choco  almond "
              }
            }
  
          ]
        }
      ]
    }
 
  ]
}

如果我搜索“Pens”(即“categoryName”),那么結果數組應該是這樣的

{
  "data": [
    {
      "categoryId": "1",
      "categoryName": "Pens",
      "subcat": [
        {
          "SubCatId": "1",
          "SubCategoryName": "Classic Cakes",
          "item": [
            {
              "DisplayName": "Excellent 500gms",
              "ItemData": {
                "ItemName": "Excellent"
              }
            },
            {
              "DisplayName": "choco vanila  500gms",
              "ItemData": {
                "Id": "26",
                "ItemName": "choco vanila "
              }
            }
          ]
        },
                {
          "SubCatId": "2",
          "SubCategoryName": "Classic Cakes2",
          "item": [
            {
              "DisplayName": "xyz 500gms",
              "ItemData": {
                "ItemName": "xyz"
              }
            },
            {
              "DisplayName": "abc  500gms",
              "ItemData": {
                "Id": "26",
                "ItemName": "choco vanila "
              }
            }
          ]
        }
      ]
    }
 
  ]
}

你可以做的是在過濾器內做一個過濾器。

var filter = "Pens";
var result = d["data"].filter(c=>
c.categoryName==filter ||
c.subcat.filter(s => s.SubCategoryName == filter).length > 0 ||
c.subcat.filter(i => i.item.filter(it => it.ItemData.ItemName == filter).length > 0).length > 0
);
console.log(result)  

其中變量“d”是 json/object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM