递归筛选具有不同属性的对象数组



我有一个对象和其他数组。我需要过滤何时在主数组匹配 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" }
]
}
]

我尝试了这些例子,但没有成功。 递归筛选对象数组

我做错了什么?

感谢您的帮助。

最初尝试的主要问题是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));

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

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

这里不需要递归。使用一个简单的过滤器,如下所示:

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'或任何Produtos有DescriaoProduto == 'AAS'。

最新更新