递归过滤具有不同属性的对象数组 [英] Recursively filter array of objects with different properties

查看:61
本文介绍了递归过滤具有不同属性的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含其他数组的对象.我需要在主数组匹配 RazaoSocial:"AAS" 或 Produtos:[{Descricao:"AAS"}]

时进行过滤

这是我的代码:

var input = [{RazaoSocial: 'AAS',产品: [{ DescricaoProduto: 'XXX', id:12, other:"other text" },{ DescricaoProduto: 'YYY', id:12, other:"other text" }]},{RazaoSocial:'我找到了你',产品: [{ 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',产品: [{ 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")) 返回真如果(o.Produtos.DescricaoProduto){console.log(o.Produtos);返回 (o.Produtos = o.Produtos.filter(f)).length}})console.log(JSON.stringify(res, null, 2));

//结果

<预><代码>[{"RazaoSocial": "AAS",产品":[{"DescricaoProduto": "XXX",身份证":12,"other": "其他文本"},{"DescricaoProduto": "YYYAAS",身份证":12,"other": "其他文本"}]}]

为什么没有返回带有我找到你"的对象?我很期待

<预><代码>[{RazaoSocial: 'AAS',产品: [{ DescricaoProduto: 'XXX', id: 12, other: "other text" },{ DescricaoProduto: 'YYY', id: 12, other: "other text" }]},{RazaoSocial:'我找到了你',产品: [{ 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" }]}]

我尝试了其中的示例,但没有成功.递归过滤对象数组

我做错了什么?

感谢您的帮助.

解决方案

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

o.Produtos 是一个数组,因此您必须先获得索引才能访问 o.Produtos.DescricaoProduto.

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

我添加了您可以比较的递归和非递归方法.

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

var input = [{RazaoSocial: 'AAS',产品: [{ DescricaoProduto: 'XXX', id:12, other:"other text" },{ DescricaoProduto: 'YYY', id:12, other:"other text" }]},{RazaoSocial:'我找到了你',产品: [{ 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',产品: [{ DescricaoProduto: 'AB', id:12, other:"other text" },{ DescricaoProduto: 'CD', id:12, other:"other text" },]},];var useRecursive = true,资源;如果(使用递归){var findValue = 函数 (str) {返回函数(o){返回 Object.values(o).filter(function (value) {如果 (Array.isArray(value)) {返回值.filter(findValue(str)).length >0} 别的 {返回值.toString().includes(str)}}).长度>0;};};res = input.filter(findValue("AAS"))} 别的 {res = input.filter(function f(o) {if (o.RazaoSocial.includes("AAS")) 返回真如果(o.Produtos.length){返回 o.Produtos.filter(function(o1) {返回 o1.DescricaoProduto.includes("AAS");}).长度;}})}console.log(JSON.stringify(res, null, 2));

I have a object with others arrays. I need to filter when in the main array match RazaoSocial:"AAS" or Produtos:[{Descricao:"AAS"}]

That's my code:

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));

//result

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

Why the object with "i found you" it's not returned ? I was expeting

[
        {
            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" }
            ]
        }
    ]

I tried the examples in, but with no success. Recursively filter array of objects

What I'm doing wrong ?

thanks for the help.

解决方案

The main problem with the original attempt was the if (o.Produtos.DescricaoProduto) {

o.Produtos is an array so you cannot access o.Produtos.DescricaoProduto without first getting an index.

So since o.Produtos is an array we can go ahead and filter it like we did the original input.

I've added both a recursive and non-recursive approach you could compare.

The recursive solution is entirely generic and can find any given value.

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));

这篇关于递归过滤具有不同属性的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆