深度过滤对象数组Javascript

时间:2018-01-15 21:22:59

标签: javascript arrays json lodash

我们试图将这个问题包围一段时间,似乎无法得到我们正在寻找的东西。我们尝试了很多循环并使用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

删除任何对象

2 个答案:

答案 0 :(得分:0)

我通常选择使用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等关键功能都包含在内,而Ramda的is(Array) / is(Object)非常相似到lodash的isArray / isObject

答案 1 :(得分:0)

If you are using lodash, I suggest to use filterDeep method from deepdash:

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

Here is a test for your case