Javascript 过滤阵列';s对象和返回参数

Javascript 过滤阵列';s对象和返回参数,javascript,jquery,arrays,reactjs,Javascript,Jquery,Arrays,Reactjs,我有两个阵列: 名为成分的数组1包含字符串值列表: Ingredients = [ "vodka", "orange juice", "lemon juice", "pineapple juice", "apple juice", "lime juice", "gin", "whiskey", &quo

我有两个阵列:

名为成分的数组1包含字符串值列表:

Ingredients = [
  "vodka",
  "orange juice",
  "lemon juice",
  "pineapple juice",
  "apple juice",
  "lime juice",
  "gin",
  "whiskey",
  "rum",
  "triple sec",
]
数组2称为鸡尾酒,包含对象列表,每个对象都有一个成分字段和一些值:

Cocktails:  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",
}
]
我正在遍历鸡尾酒中的所有对象,并从配料数组中仅返回缺少1种配料(值)的对象。我还使用以下函数保存缺少的成分

const cocktailPartialMatch = Cocktails.filter(function (item) {     
      var itemIngredient = item.ingredients.toLowerCase().split(",");
      var matchCount = 0;
      var missingIngredient = [];

      itemIngredient.forEach((element) =>
        Ingredients.indexOf(element.trim()) >= 0
          ? matchCount++
          : missingIngredient.push(element)
      );
      // return item if there is only 1 missing ingredient
      return itemIngredient.length - matchCount == 1;
      
    });
除了我还想保存缺少的成分外,所有的都正常工作

此函数的结果是原始鸡尾酒数组的筛选列表。 我的目标是获得一个新数组,其中包含过滤后的鸡尾酒对象及其缺少的成分,例如

cocktailPartialMatch = [{object} {missingIngredient}, {object} {missingIngredient}...]

I'm correctly fetching missingIngredient[] but failing to understand how to return it for each object.
这是不可能的:

cocktailPartialMatch=[{object}{MissingRedient},{object}{MissingRedient}…]

必须将这两个对象存储在嵌套数组中,如下所示:

cocktailPartialMatch=[[{object}{MissingRedient}],{object}{MissingRedient}]…]

请尝试以下代码:

filtered = [];

for (c of cocktails) {
    missingIngredients = [];    
    
    c.ingredients.split(',').forEach(ingredient => {
        ingredients.map(i => i.trim().toLowerCase());
        if (!ingredients.includes(ingredient.trim().toLowerCase())) {
            missingIngredients.push(ingredient);
        }
    });
    
    if (missingIngredients.length === 1) {
        entry = [c, missingIngredients[0]];
        filtered.push(entry);
    }
}
const成分=[
“伏特加”,
“橙汁”,
“柠檬汁”,
“菠萝汁”,
“苹果汁”,
“酸橙汁”,
“杜松子酒”,
“威士忌”,
“朗姆酒”,
“三秒”,
]
康斯特鸡尾酒=[
{
“酒精”:“真实”,
“酒鬼”:“1101”,
“配料”:“杜松子酒、酸橙糖浆、酸橙”,
},
{
“酒精”:“真实”,
“酒鬼”:“1102”,
“配料”:“伏特加、橙汁”,
},
{
“酒精”:“真实”,
“酒鬼”:“1103”,
“配料”:“朗姆酒、可乐”,
},
{
“酒精”:“真实”,
“酒鬼”:“1104”,
“配料”:“甜苦艾酒、坎帕里、普罗赛科”,
},
{
“酒精”:“真实”,
“酒鬼”:“1105”,
“配料”:“杜松子酒、橄榄汁、橄榄、干苦艾酒”,
},
{
“酒精”:“真实”,
“酒鬼”:“1106”,
“配料”:“伏特加、三倍秒、酸橙汁”,
},
{
“酒精”:“真实”,
“酒鬼”:“1107”,
“配料”:“伏特加、覆盆子利口酒、菠萝汁”,
}
]
过滤=[];
对于(c组鸡尾酒){
缺失要素=[];
c、 配料。拆分(',')。forEach(配料=>{
map(i=>i.trim().toLowerCase());
如果(!配料.includes(配料.trim().toLowerCase())){
缺失成分。推(成分);
}
});
if(missingElements.length==1){
条目=[c,缺失要素[0]];
过滤、推送(进入);
}
}
console.log(过滤)您可以获取并获取包含缺失项数组的新对象

const
成分=[“伏特加”、“橙汁”、“柠檬汁”、“菠萝汁”、“苹果汁”、“酸橙汁”、“杜松子酒”、“威士忌”、“朗姆酒”、“三倍秒”],
鸡尾酒=[{酒精:真的,酒鬼:“1101”,成分:“杜松子酒,莱姆糖浆,莱姆”},{酒精:真的,酒鬼:“1102”,成分:“伏特加,橙汁”},{酒精:真的,酒鬼:“1103”,成分:“朗姆酒,可乐”},{酒精:真的,酒鬼:“1104”,成分:“甜苦艾酒,坎帕里,普罗塞科”},{酒精:真的,酒鬼:“1105”,配料:“杜松子酒,橄榄汁,橄榄,干苦艾酒”},{酒精:真的,酒鬼:“1106”,配料:“伏特加,三倍秒,酸橙汁”},{酒精:真的,酒鬼:“1107”,配料:“伏特加,覆盆子利口酒,菠萝汁”},
cocktailPartialMatch=cocktails.flatMap(项目=>{
常数
missingCredit=项目。成分
.toLowerCase()
.拆分(“,”)
.map(s=>s.trim())
.过滤器((元素)=>!成分。包括(元素));

return missingCredition.length我建议将
cocktailPartialMatch
转换为使用一个实用函数,返回缺失成分数组,然后使用reduce返回过滤后的鸡尾酒数组和缺失成分数组

const cocktailPartialMatch = Cocktails.reduce((cocktails, cocktail) => {
  const missingIngredients = cocktail.ingredients
  .toLowerCase()
  .split(",")
  .filter((ingredient) => !Ingredients.includes(ingredient));

  if (missingIngredients.length === 1)
    cocktails.push([cocktail, missingIngredients]);

  return cocktails;
}, []);
const成分=[
“伏特加”,
“橙汁”,
“柠檬汁”,
“菠萝汁”,
“苹果汁”,
“酸橙汁”,
“杜松子酒”,
“威士忌”,
“朗姆酒”,
“三秒”
];
康斯特鸡尾酒=[
{
酒鬼:“真的”,
德林基德:“1101”,
配料:“杜松子酒、酸橙糖浆、酸橙”
},
{
酒鬼:“真的”,
德林基德:“1102”,
成分:“伏特加、橙汁”
},
{
酒鬼:“真的”,
德林基德:“1103”,
配料:“朗姆酒、可乐”
},
{
酒鬼:“真的”,
德林基德:“1104”,
成分:“甜苦艾酒、坎帕里、普罗赛科”
},
{
酒鬼:“真的”,
德林基德:“1105”,
配料:“杜松子酒、橄榄汁、橄榄、干苦艾酒”
},
{
酒鬼:“真的”,
德林基德:“1106”,
成分:“伏特加、三倍秒、酸橙汁”
},
{
酒鬼:“真的”,
德林基德:“1107”,
成分:“伏特加、覆盆子利口酒、菠萝汁”
}
];
const cocktailPartialMatch=鸡尾酒。减少((鸡尾酒,鸡尾酒)=>{
常量缺失成分=鸡尾酒成分
.toLowerCase()
.拆分(“,”)
.过滤器((配料)=>!配料。包括(配料));
if(missingElements.length==1)
鸡尾酒。推送([鸡尾酒,遗漏成分]);
返回鸡尾酒;
}, []);

console.log(cocktailPartialMatch)
你有想要的结果的完整结果吗?差不多了……我得到了以下输出格式
cocktailPartialMatch=[[{object}missingCredit],{object}missingCredit]…]
missingCredit作为“覆盆子利口酒”。我的想法是获取这两个对象并通过索引[0]和[1]访问它们。您可以这样访问它们:
cocktailPartialMatch[0][0]
cocktailPartialMatch[0][1]
您的解决方案非常适合我所寻找的对象,因为我甚至不必更改渲染代码,因为
MissingCredition