亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 從數組中獲取購物清單

Javascript 從數組中獲取購物清單

森林海 2023-03-03 16:07:09
我是 javascript 的新手,所以我什至不知道從哪里開始。請有人可以幫助我。我有這個配料表: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"];這就是我想要獲得的:// desired output:const ShoppingList = [  {    produceOutput: [      {        val: "garlic, minced",        amount: "8 cloves ",      },    ],    spicesOutput: [      {        val: "paprika",        amount: "? tsp  ",      },      {        val: "onion",        amount: "1",      },    ],    NoCategoryOutput: [      {        val: "fine sea salt",        amount: "? tsp",      },    ],  },];
查看完整描述

4 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

正如這里的其他人建議使用 modernreduce或數組方法......以防萬一您需要支持舊瀏覽器或更“經典”的實現,您可以使用includes帶有數組方法的簡單循環。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 中有一些重復的成分 - 它們不會加總它們的數量。為此,您需要以某種數字格式(例如十進制數)提供或轉換每個金額。此外,如果當前成分已經在列表中,并且在這種情況下 - 添加它們的數量,而不是簡單的push會有一些額外的邏輯檢查。


查看完整回答
反對 回復 2023-03-03
?
飲歌長嘯

TA貢獻1951條經驗 獲得超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: []

})


查看完整回答
反對 回復 2023-03-03
?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

這是一種稍微更加參數化的方法。我將不同的食物類別組合到一個對象中cat,并允許成分的部分匹配(單數匹配復數):


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


查看完整回答
反對 回復 2023-03-03
?
Qyouu

TA貢獻1786條經驗 獲得超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);


查看完整回答
反對 回復 2023-03-03
  • 4 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號