【问题标题】:Recursively filter array of objects with different properties递归过滤具有不同属性的对象数组
【发布时间】:2018-02-20 01:21:03
【问题描述】:

我有一个带有其他数组的对象。当主数组匹配 RazaoSocial:"AAS" 或 Produtos:[{Descricao:"AAS"}]

时,我需要过滤

这是我的代码:

var input = [
  {
    RazaoSocial: 'AAS',
    Produtos: [
    { DescricaoProduto: 'XXX', id:12, other:"other text" },
      { DescricaoProduto: 'YYY', id:12, other:"other text" }
    ]
  },
  {
    RazaoSocial: 'I found you',
    Produtos: [
      { DescricaoProduto: 'AAS', id:12, other:"other text" },
      { DescricaoProduto: 'Miss8', id:12, other:"other text" },
      { DescricaoProduto: 'Miss8', id:12, other:"other text" },
      { DescricaoProduto: 'Miss8', id:99, other:"other text" }      
    ]
  },
  {
    RazaoSocial: 'bla',
    Produtos: [
        { DescricaoProduto: 'AB', id:12, other:"other text" },
        { DescricaoProduto: 'CD', id:12, other:"other text" },
    ]
  },  
];

var res = input.filter(function f(o) {
    if (o.RazaoSocial.includes("AAS")) return true
    if (o.Produtos.DescricaoProduto) {
        console.log(o.Produtos);
        return (o.Produtos = o.Produtos.filter(f)).length
    }
})
 console.log(JSON.stringify(res, null, 2));

//结果

[
  {
    "RazaoSocial": "AAS",
    "Produtos": [
      {
        "DescricaoProduto": "XXX",
        "id": 12,
        "other": "other text"
      },
      {
        "DescricaoProduto": "YYYAAS",
        "id": 12,
        "other": "other text"
      }
    ]
  }
]

为什么没有返回带有“我找到你”的对象?我在努力

[
        {
            RazaoSocial: 'AAS',
            Produtos: [
                { DescricaoProduto: 'XXX', id: 12, other: "other text" },
                { DescricaoProduto: 'YYY', id: 12, other: "other text" }
            ]
        },
        {
            RazaoSocial: 'I found you',
            Produtos: [
                { DescricaoProduto: 'AAS', id: 12, other: "other text" },
                { DescricaoProduto: 'Miss8', id: 12, other: "other text" },
                { DescricaoProduto: 'Miss8', id: 12, other: "other text" },
                { DescricaoProduto: 'Miss8', id: 99, other: "other text" }
            ]
        }
    ]

我尝试了其中的示例,但没有成功。 Recursively filter array of objects

我做错了什么?

感谢您的帮助。

【问题讨论】:

    标签: javascript arrays recursion filter


    【解决方案1】:

    最初尝试的主要问题是if (o.Produtos.DescricaoProduto) {

    o.Produtos 是一个数组,因此如果没有先获取索引,您将无法访问 o.Produtos.DescricaoProduto

    因此,由于o.Produtos 是一个数组,我们可以像处理原始输入一样继续过滤它。

    我添加了递归和非递归方法,您可以进行比较。

    递归解决方案是完全通用的,可以找到任何给定的值。

    var input = [
      {
        RazaoSocial: 'AAS',
        Produtos: [
          { DescricaoProduto: 'XXX', id:12, other:"other text" },
          { DescricaoProduto: 'YYY', id:12, other:"other text" }
        ]
      },
      {
        RazaoSocial: 'I found you',
        Produtos: [
          { DescricaoProduto: 'AAS', id:12, other:"other text" },
          { DescricaoProduto: 'Miss8', id:12, other:"other text" },
          { DescricaoProduto: 'Miss8', id:12, other:"other text" },
          { DescricaoProduto: 'Miss8', id:99, other:"other text" }      
        ]
      },
      {
        RazaoSocial: 'bla',
        Produtos: [
            { DescricaoProduto: 'AB', id:12, other:"other text" },
            { DescricaoProduto: 'CD', id:12, other:"other text" },
        ]
      },  
    ];
    
    var useRecursive = true, 
        res;
    if (useRecursive) {
      var findValue = function (str) {
          return function(o) {
              return Object.values(o).filter(function (value) {
                  if (Array.isArray(value)) {
                      return value.filter(findValue(str)).length > 0
                  } else {
                      return value.toString().includes(str)
                  }
              }).length > 0;
          };
      };
      res = input.filter(findValue("AAS"))
    
    } else {
      res = input.filter(function f(o) {
        if (o.RazaoSocial.includes("AAS")) return true
        if (o.Produtos.length) {
            return o.Produtos.filter(function(o1) {
              return o1.DescricaoProduto.includes("AAS");
            }).length;
        }
      })
    }
    console.log(JSON.stringify(res, null, 2));

    【讨论】:

    • 想留下一些关于您的代码为何有效的评论?如果是这样,将考虑投票。
    【解决方案2】:
    var res = input.map(function f(o) {
    var _ret = false;
    if (o.RazaoSocial.includes("AAS")) _ret  = true
    if (o.Produtos.DescricaoProduto) {
        console.log(o.Produtos);
        _ret  = (o.Produtos = o.Produtos.filter(f)).length
    }
    return _ret; 
    })
    

    我还没有测试这个利用上面的

    【讨论】:

      【解决方案3】:

      这里不需要递归。使用类似的简单过滤器:

      var res = input.filter(function (o) {
       return o.RazaoSocial.includes("AAS") ||
         o.Produtos && o.Produtos.find(function (p) {
           return p.DescricaoProduto === 'AAS';
       }));
      console.log(JSON.stringify(res, null, 2));
      

      至少我认为这就是你所追求的!它应该为您留下数组中 RazaoSocial 匹配 'AAS' 或任何 Produto 具有 DescriaoProduto == 'AAS' 的所有对象。

      【讨论】:

        猜你喜欢
        • 2016-11-03
        • 1970-01-01
        • 2020-06-30
        • 2021-07-07
        • 2019-10-03
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        • 2020-01-10
        相关资源
        最近更新 更多