簡體   English   中英

對象的深度過濾數組Javascript

[英]Deep Filtering Array of Objects Javascript

我們正在努力解決這一問題,似乎無法獲得我們想要的東西。 我們已經嘗試了很多循環,並使用lodash庫使之成為可能,但沒有成功。 我們要遍歷數組,並返回所有具有checked:true屬性的對象和子對象,或者刪除具有checked: false的對象。 有多個層次,我們無法返回我們要尋找的東西

types: [
  {
    name: 'Dresses',
    checked: true,
    collections: [
      {
        name: 'My Collection',
        checked: true,
        variations: [
          { 
            size: 'XXS',
            checked: true

          },
          { 
            size: 'XS',
            checked: false

          }
        ]
      },
      {
        name: 'False Collection',
        checked: false,
        variations: [
          { 
            size: 'XXS',
            checked: false

          },
          { 
            size: 'XS',
            checked: false

          }
        ]
      }
    ]
  },
  {
    name: 'Tops',
    checked: true,
    collections: [
      {
        name: 'Another Collection',
        checked: true,
        variations: [
          { 
            size: 'XXS',
            checked: false

          },
          { 
            size: 'XS',
            checked: true

          }
        ]
      }
    ]
  }
]

我們嘗試生成的預期輸出為:

types: [
      {
        name: 'Dresses',
        checked: true,
        collections: [
          {
            name: 'My Collection',
            checked: true,
            variations: [
              { 
                size: 'XXS',
                checked: true

              }
            ]
          }
        ]
      },
      {
        name: 'Tops',
        checked: true,
        collections: [
          {
            name: 'Another Collection',
            checked: true,
            variations: [
              { 
                size: 'XS',
                checked: true

              }
            ]
          }
        ]
      }
    ]

這將刪除所有已checked: false屬性的對象checked: false

我通常選擇使用Ramda來解決此類問題。 (免責聲明:我是它的作者之一。)使用Ramda,您可以這樣操作:

 const types = [{"checked": true, "collections": [{"checked": true, "name": "My Collection", "variations": [{"checked": true, "size": "XXS"}]}], "name": "Dresses"}, {"checked": true, "collections": [{"checked": true, "name": "Another Collection", "variations": [{"checked": true, "size": "XS"}]}], "name": "Tops"}] const {is, filter, pipe, toPairs, map, fromPairs} = R; const onlyChecked = (obj) => is(Array, obj) ? filter(onlyChecked, obj) : is(Object, obj) ? pipe( toPairs, filter(([key, val]) => val.checked !== false), map(([key, val]) => [key, onlyChecked(val)]), fromPairs )(obj) : obj; console.log(onlyChecked(types)) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> 

這應該很容易移植到lodash,因為諸如fromPairstoPairs類的關鍵函數都包含在內,並且toPairsis(Array) / is(Object)與lodash的isArray / isObject非常相似。

如果您正在使用lodash,我建議使用filterDeep方法從deepdash

var filtrate = _.filterDeep(types, function(value, key, parent) {
  if (parent.checked) return true;
});

這是您的情況的檢驗

暫無
暫無

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

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