[英]Filtering nested nested array
嗨有这个数组:
[ {
"Navn": "Long Island Iced Tea",
"Nummer": "2",
"Glas i ml": "250",
"Instruktioner": "",
"a": "Hæld is i glasset",
"b": "pynt med en skive lime",
"Ingredienser": [
{
"Type": "Spiritus",
"Del1": [
{
"Cointreau": 20
}
],
"Del2": [
{
"Gin": 20
}
],
"Del3": [
{
"Rom_Lys": 20
}
],
"Del4": [
{
"Tequila": 20
}
],
"Del5": [
{
"Vodka": 20
}
]
},
{
"Type": "Vand/Juice",
"Del1": [
{
"Cola": 40
}
],
"Del2": [
{
"Sprite": 20
}
]
},
{
"Type": "Mixer",
"Del1": [
{
"Lime_Sirup": 20
}
]
}
]
}]
它用于鸡尾酒机。
我想通过“Del1”搜索(例如)“Rom_Lys”和“Cola”和“Vodka”来过滤它,然后是 output 一个具有这些规范的新数组。
我尝试搜索论坛,但似乎找不到有用的东西。 玩过过滤器和包含,但想不出任何有用的东西。
谢谢!
如果要获取包含Cola
的项目,则可以使用filter
和some
方法:
const filterWord = 'Cola';
const result = sampleData.filter(s =>
s.Ingredienser.some(s =>
s.Del1.some( e=> e[filterWord])));
console.log(result);
一个例子:
let sampleData = [ { "Navn": "Rom & Cola/ Cuba Libre", "Nummer": "0", "Glas i ml": "200", "Instruktioner": "", "a": "Hæld is i glasset", "b": "pynt med en skive citron", "Ingredienser": [ { "Type": "Spiritus", "Del1": [ { "Rom_Lys": 40 } ] }, { "Type": "Vand/Juice", "Del1": [ { "Cola": 100 } ] }, { "Type": "Mixer", "Del1": [ {} ] } ] }, { "Navn": "Isbjørn", "Nummer": "1", "Glas i ml": "200", "Instruktioner": "", "a": "Hæld is i glasset", "b": "pynt med en skive citron", "Ingredienser": [ { "Type": "Spiritus", "Del1": [ { "Vodka": 30 } ] }, { "Type": "Vand/Juice", "Del1": [ { "Sprite": 60 } ] }, { "Type": "Mixer", "Del1": [ { "Blå_Sirup": 30 } ] } ] }]; const filterWord = 'Cola'; const result = sampleData.filter(s => s.Ingredienser.some(s => s.Del1.some( e=> e[filterWord]))); console.log(result);
更新:
如果要检查多个密钥,则可以使用hasOwnProperty
方法检查 object 是否包含所需的密钥:
const filters = ['Cointreau', 'Gin', 'Rom_Lys'];
const result = sampleData.filter(s =>
s.Ingredienser.some(ingred => {
return Object.keys(ingred).some(k=> {
if (Array.isArray(ingred[k])) {
return ingred[k].some(s=> filters.some(f=> {
return s.hasOwnProperty(f);
}))
}
});
}
));
还有这个例子:
let sampleData = [ { "Navn": "Long Island Iced Tea", "Nummer": "2", "Glas i ml": "250", "Instruktioner": "", "a": "Hæld is i glasset", "b": "pynt med en skive lime", "Ingredienser": [ { "Type": "Spiritus", "Del1": [ { "Cointreau": 20 } ], "Del2": [ { "Gin": 20 } ], "Del3": [ { "Rom_Lys": 20 } ], "Del4": [ { "Tequila": 20 } ], "Del5": [ { "Vodka": 20 } ] }, { "Type": "Vand/Juice", "Del1": [ { "Cola": 40 } ], "Del2": [ { "Sprite": 20 } ] }, { "Type": "Mixer", "Del1": [ { "Lime_Sirup": 20 } ] } ] }]; const filters = ['Cointreau', 'Gin', 'Rom_Lys']; const result = sampleData.filter(s => s.Ingredienser.some(ingred => { return Object.keys(ingred).some(k=> { if (Array.isArray(ingred[k])) { return ingred[k].some(s=> filters.some(f=> { return s.hasOwnProperty(f); })) } }); } )); console.log(result);
尝试使用以下代码过滤数组。
var filtered = arr.filter(function(unique) {
for (var index = 0; index < unique.Ingredienser.length; index++) {
if (unique.Ingredienser[index].Del1[0].hasOwnProperty(selectedchoice)) {
return true;
}
}
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.