4 回答

TA貢獻1900條經(jīng)驗 獲得超5個贊
正如這里的其他人建議使用 modernreduce或數(shù)組方法......以防萬一您需要支持舊瀏覽器或更“經(jīng)典”的實現(xiàn),您可以使用includes帶有數(shù)組方法的簡單循環(huán)。forEachforindexOf
const Ingris = [{ val: "onion,", amount: "1", },
{ val: "paprika", amount: "? tsp", },
{ val: "yogurt", amount: "1/2 Cup", },
{ val: "fine sea salt", amount: "? tsp ", },
];
var spices = ["paprika", "parsley", "peppermint", "poppy seed", "rosemary"];
var meats = ["steak", "ground beef", "stewing beef", "roast beef", "ribs"];
var dairy = ["milk", "eggs", "cheese", "yogurt"];
var produce = ["peppers", "radishes", "onions", "tomatoes"];
ShoppingList = {
produceOutput: [],
spicesOutput: [],
meatsOutput: [],
dairyOutput: [],
NoCategoryOutput: [],
};
for (var i = 0; i < Ingris.length; i++) {
var ingredient = Ingris[i];
if (spices.indexOf(ingredient.val) >= 0) {
ShoppingList.spicesOutput.push(ingredient);
} else if (meats.indexOf(ingredient.val) >= 0) {
ShoppingList.meatsOutput.push(ingredient);
} else if (dairy.indexOf(ingredient.val) >= 0) {
ShoppingList.dairyOutput.push(ingredient);
} else if (produce.indexOf(ingredient.val) >= 0) {
ShoppingList.produceOutput.push(ingredient);
} else {
ShoppingList.NoCategoryOutput.push(ingredient);
}
}
console.log(ShoppingList)
還要記住,如果在 Ingris 中有一些重復(fù)的成分 - 它們不會加總它們的數(shù)量。為此,您需要以某種數(shù)字格式(例如十進制數(shù))提供或轉(zhuǎn)換每個金額。此外,如果當(dāng)前成分已經(jīng)在列表中,并且在這種情況下 - 添加它們的數(shù)量,而不是簡單的push會有一些額外的邏輯檢查。

TA貢獻1951條經(jīng)驗 獲得超3個贊
您可以使用基本的減速器來進行此類操作。
const categorizedOutput = Ingris.reduce((acc, cur) => {
if (spices.includes(cur.val)) {
acc.spices.push(cur);
} else if (meats.includes(cur.val)) {
acc.meats.push(cur);
} else if (dairy.includes(cur.val)) {
acc.dairy.push(cur);
} else if (produce.includes(cur.val)) {
acc.produce.push(cur);
} else {
acc.other.push(cur);
}
return acc;
}, {
spices: [],
meats: [],
dairy: [],
produce: [],
other: []
})

TA貢獻1895條經(jīng)驗 獲得超7個贊
這是一種稍微更加參數(shù)化的方法。我將不同的食物類別組合到一個對象中cat,并允許成分的部分匹配(單數(shù)匹配復(fù)數(shù)):
const cat={
spices: ["paprika", "parsley", "peppermint", "poppy seed", "rosemary","fine sea salt"],
meats: ["steak", "ground beef", "stewing beef", "roast beef", "ribs"],
dairy: ["milk", "eggs", "cheese", "yogurt"],
produce: ["peppers", "radishes", "onions", "tomatoes"]};
var ingredients=[
{"val":"onion" , "amount":"1"},
{"val":"paprika" , "amount":"? tsp"},
{"val":"yogurt" , "amount":"1/2 Cup"},
{"val":"fine sea salt", "amount":"? tsp"}
];
const shop=ingredients.reduce((acc,ing)=>{
Object.entries(cat).some(([ca,pr])=>
pr.find(p=>p.indexOf(ing.val)>-1) &&
(acc[ca]=acc[ca]||[]).push(ing) )
|| (acc.other=acc.other||[]).push(ing);
return acc;
}, {});
console.log(shop);

TA貢獻1786條經(jīng)驗 獲得超11個贊
var spices = ["paprika", "parsley", "peppermint", "poppy seed", "rosemary"];
var meats = ["steak", "ground beef", "stewing beef", "roast beef", "ribs"];
var dairy = ["milk", "eggs", "cheese", "yogurt"];
var produce = ["peppers", "radishes", "onions", "tomatoes"];
var ingredients=[
{"val":"onion," , "amount":"1"},
{"val":"paprika" , "amount":"? tsp"},
{"val":"yogurt" , "amount":"1/2 Cup"},
{"val":"fine sea salt", "amount":"? tsp"}
];
var shoppingList={spices:[], meats:[], dairy:[], produce:[], other:[]};
ingredients.forEach(ingredient=>{
if(spices.includes(ingredient.val)) shoppingList.spices.push(ingredient);
else if(meats.includes(ingredient.val)) shoppingList.meats.push(ingredient);
else if(dairy.includes(ingredient.val)) shoppingList.dairy.push(ingredient);
else if(produce.includes(ingredient.val)) shoppingList.produce.push(ingredient);
else shoppingList.other.push(ingredient);
});
console.log(shoppingList);
添加回答
舉報