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

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

根據某些字段的值匹配2個數組

根據某些字段的值匹配2個數組

蠱毒傳說 2023-03-24 13:55:47
我在名為myBar的數組中有以下成分列表。該成分包含在屬性“名稱”中myBar:  Array [  bar {    "category": "spirits",    "id": "1",    "ingredientId": "2003",    "name": "vodka",  },  bar {    "category": "juice",    "id": "2",    "ingredientId": "2017",    "name": "orange juice",  },  bar {    "category": "juice",    "id": "3",    "ingredientId": "2020",    "name": "lemon juice",  },  bar {    "category": "juice",    "id": "4",    "ingredientId": "2027",    "name": "Pineapple Juice",  },  bar {    "category": "juice",    "id": "5",    "ingredientId": "2018",    "name": "apple Juice",  },  bar {    "category": "juice",    "id": "6",    "ingredientId": "2025",    "name": "Lime Juice",  },  bar {    "category": "spirits",    "id": "7",    "ingredientId": "2001",    "name": "gin",  },  bar {    "category": "spirits",    "id": "8",    "ingredientId": "2005",    "name": "whiskey",  },  bar {    "category": "spirits",    "id": "9",    "ingredientId": "2002",    "name": "rum",  },]我還有一個名為cocktailList 的第二個數組,其中包含雞尾酒配方列表。在這里,成分包含在“成分”字段中。cocktailList:  Array [Object {    "alcoholic": "true",        "drinkId": "1101",     "ingredients": " gin, lime syrup, lime",},Object {    "alcoholic": "true",        "drinkId": "1102",     "ingredients": "vodka, orange juice",},Object {    "alcoholic": "true",        "drinkId": "1103",     "ingredients": "rum, coke",},Object {    "alcoholic": "true",        "drinkId": "1104",     "ingredients": "sweet vermouth, campari, prosecco",},Object {    "alcoholic": "true",        "drinkId": "1105",     "ingredients": "Gin, Olive Juice, Olives, Dry vermouth",},Object {    "alcoholic": "true",        "drinkId": "1106",     "ingredients": " Vodka, Triple Sec, Lime Juice",},Object {    "alcoholic": "true",        "drinkId": "1107",     "ingredients": " vodka, Raspberry Liqueur, Pineapple Juice",}]我正在嘗試根據配料的含量來檢查我可以制作哪些雞尾酒。這個想法是迭代每個食譜(在cocktailList中)并檢查所有成分是否也是我在myBar中的成分列表的一部分。例如,“drinkId”:“1102”應該返回 true,因為我的myBar中有所有成分(伏特加和橙汁) ,而“drinkId”:“1101”應該返回 false,因為我既沒有酸橙糖漿也沒有酸橙。
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

從第一個數組創建一個成分名稱數組,以便您可以快速搜索直接數組中的成分。


// arr1 is your list of ingredients in your bar.

var arrName  = [];

arr1.forEach(element => arrName.push(element.name));


console.log(arrName);

Output:

["vodka", "orange juice", "lemon juice", "Pineapple Juice", "apple Juice", "Lime Juice", "gin", "whiskey", "rum"]

現在遍歷cocktailList并在 中搜索每種成分arrName。如果匹配元素的數量等于雞尾酒中成分的數量,那么它就是一個匹配項。


var finalArr = arr2.filter(function(obj){

    var arrIngredient = obj.ingredients.split(',');

    var intMatch = 0;

    

    arrIngredient.forEach(element => arrName.indexOf(element.trim()) >= 0 ? intMatch++ : '');

    

    return arrIngredient.length == intMatch;

});


console.log(finalArr);

Output:

[

    {

         alcoholic: "true"

         drinkId: "1102"

         ingredients: "vodka, orange juice"

    }

]


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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