简体   繁体   English

如果他的属性匹配数组中的所有项目(例如食谱的成分),则返回 object

[英]return object if his property matches all the items inside an array (ex ingredients for recipes)

I'm learning js and I was trying to filter recipes based on the ingredients I have inside an array but the check done with Array.every() seems to not work, what am I doing wrong?我正在学习 js,我试图根据我在数组中的成分过滤食谱,但是用 Array.every() 完成的检查似乎不起作用,我做错了什么? Here's the code, thanks all!这是代码,谢谢大家!

const plates = [
  {
    name: "carbonara",
    ingredients: ["eggs", "milk", "pepper", "pasta", "cheese"],
    time: 30,
    difficulty: "easy",
  },
  {
    name: "oil_pepper",
    ingredients: ["oil", "pepper", "pasta", "garlic"],
    time: 20,
    difficulty: "medium",
  },
  {
    name: "tomato",
    ingredients: ["pasta", "tomato", "garlic", "onion"],
    time: "30",
    difficulty: "hard",
  },
];

const fridge = ["oil", "pepper", "pasta", "garlic"];


let filteredPlates = [];

const filter = () => {
  plates.forEach((element) =>
    fridge.forEach((element2) => {
      if (
        element.ingredients.every((element3) =>
          element3.includes(element2.toString())
        )
      ) {
        filteredPlates.push(element);
        return filteredPlates;
      }
    })
  );
};

filter();

console.log(filteredPlates);

You are over-complicating the problem.你把问题复杂化了。 You just need to check for each plate, whether every ingredient in that plate's recipe is included in the fridge.您只需要检查每个盘子,该盘子食谱中的每种成分是否都包含在冰箱中。 You can use Array.filter for this purpose:您可以为此目的使用Array.filter

 const plates = [ { name: "carbonara", ingredients: ["eggs", "milk", "pepper", "pasta", "cheese"], time: 30, difficulty: "easy", }, { name: "oil_pepper", ingredients: ["oil", "pepper", "pasta", "garlic"], time: 20, difficulty: "medium", }, { name: "tomato", ingredients: ["pasta", "tomato", "garlic", "onion"], time: "30", difficulty: "hard", }, ]; const fridge = ["oil", "pepper", "pasta", "garlic"]; const filteredPlates = plates.filter(plt => plt.ingredients.every(ing => fridge.includes(ing))) console.log(filteredPlates)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回对象数组内的所有项目 - How to return all items inside array of object 如果它与 id 匹配,如何在 object 中的 object 数组中添加一个属性 - how to add a property in object inside array of object if it matches the id Mongoose - 仅当条件匹配数组中的所有对象时才返回 object - Mongoose - Return object only if condition matches all the objects in array 如果属性匹配字符串,则 JSX 过滤数组中的项目,否则显示所有项目 - JSX filter items in array if property matches string, otherwise show all items 如何在 object 中过滤 arrays 并返回其中包含最多项目的数组? - how to filter arrays inside an object and return the array with the most items inside? 如何访问Match数组中的所有Match - How to access all the matches inside matches array 如何根据 JS 中的 object 属性对数组内的项目进行排序 - How to order items inside array based on object property in JS 什么是搜索食谱数组并匹配某些成分列表上的多个单词的有效方法 - What is an efficient way to search through an array of recipes and match multiple words on some ingredients list 将数组中的所有 object 项与 JavaScript 中的动态属性相加 - Sum all object items in array with dynamic property in JavaScript 是否可以从数组中找到 object 并返回 object 而不是他的参考? - is it possible to find an object from an array and return an object not his reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM