具有多个搜索标准的Jquery grep或map对象数组

我在JSON中有一个价目表:

{Products: [{AdminID: 137, ProduktID: "07.1434", itemName: "Repaplast", itemColor: "0000, 5030", MalKode: "1-3", …}{AdminID: 6, ProduktID: "07.1436", itemName: "Repaplast grå", itemColor: "0070", MalKode: "1-3", …}{AdminID: 146, ProduktID: "90.0905", itemName: "Mixer Gun", itemColor: null, MalKode: "", …}{AdminID: 89, ProduktID: "02.0135", itemName: "Repaplast Primer NEW FORMULA", itemColor: "", MalKode: "5-3", …}]} 

该列表大约有400个产品。

假设我想限制列表,只显示基于ProduktID字段的特定产品(这是唯一的)。 假设我有一个产品清单:

07.1438,01,1340,05,04531,02.0135

逗号是分隔符。

我如何基于这些输入.grep.map对象数组? 我是否必须逐个搜索,然后将结果合并到另一个对象中? 或者有更快的方法吗?

您可以使用array#filterarray#find 。 它将导致根据product ids字符串过滤products

 const productIds = '07.1438, 01,1340, 05,04531, 02.0135'; const products = [{AdminID: 137, ProduktID: "07.1438", itemName: "Repaplast", itemColor: "0000, 5030", MalKode: "1-3",},{AdminID: 6, ProduktID: "07.1436", itemName: "Repaplast grå", itemColor: "0070", MalKode: "1-3",},{AdminID: 146, ProduktID: "90.0905", itemName: "Mixer Gun", itemColor: null, MalKode: "",},{AdminID: 89, ProduktID: "02.0135", itemName: "Repaplast Primer NEW FORMULA", itemColor: "", MalKode: "5-3",}]; var result = products.filter( o => productIds.split(',').find(productId => o.ProduktID === productId.trim())); console.log(result); 

使用Array.prototype.filter()Array.prototype.includes()结合使用:

 const wanted = ['07.1438', '01','1340', '05', '04531', '02.0135']; const products = [ { AdminID : 137, ProduktID: "07.1434", itemName : "Repaplast", itemColor: "0000, 5030", MalKode : "1-3" }, { AdminID : 6, ProduktID: "07.1436", itemName : "Repaplast grå", itemColor: "0070", MalKode : "1-3" }, { AdminID : 146, ProduktID: "90.0905", itemName : "Mixer Gun", itemColor: null, MalKode : "" }, { AdminID : 89, ProduktID: "02.0135", itemName : "Repaplast Primer NEW FORMULA", itemColor: "", MalKode : "5-3" } ]; const filtered = products.filter((product) => wanted.includes(product.ProduktID)); console.log("Filtered", filtered);