第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Javascript 從數(shù)組中獲取購物清單

Javascript 從數(shù)組中獲取購物清單

森林海 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  ",  },];我想根據(jù)以下這些變量對它們進行分類: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條經(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會有一些額外的邏輯檢查。


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

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: []

})


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

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


查看完整回答
反對 回復(fù) 2023-03-03
?
Qyouu

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


查看完整回答
反對 回復(fù) 2023-03-03
  • 4 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號