当前位置: 首页 > 知识库问答 >
问题:

Javascript/React本地-如何在两个不同对象数组之间进行交叉检查?

耿招
2023-03-14

我有一个应用程序将使一个食谱数据库,在那里用户将输入他有的成分,应用程序应该返回与这种组合可用的食谱,用户已经进入。

一旦我有了具有许多键/值信息的recipe对象:

 const [recipes, setRecipes] = useState([
    { id: 1, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Prato principal', name: 'pão de forma', key: '1', ingredients: ['sal', 'ovo', 'mantega', 'maionese', 'farinha'] },
    { id: 2, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Entrada', name: 'macarrao ao molho branco', key: '2', ingredients: ['macarrao', 'oleo', 'creme de leite'] },
    { id: 3, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Salada', name: 'Arroz marroquino', key: '3', ingredients: ['A', 'B', 'C'] },
    { id: 4, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Sobremesa', name: 'Bolo', key: '4', ingredients: ['farinha', 'açucar', 'etc'] },
    { id: 5, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Bolo', name: 'doce de leite', key: '5', ingredients: ['AA', 'BB', 'CC'] },
  ])
const [ingredients, setIngredients] = useState(['farinha', 'ovos', 'leite'])

const addIngredients = e => {
  e.preventDefault();
  setIngredients(prevIngredient => [...prevIngredient, name]);
}

谢了!

共有1个答案

韦熙云
2023-03-14

利用数组的方法:

const recipes = [
  { id: 1, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Prato principal', name: 'pão de forma', key: '1', ingredients: ['sal', 'ovo', 'mantega', 'maionese', 'farinha'] },
  { id: 2, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Entrada', name: 'macarrao ao molho branco', key: '2', ingredients: ['macarrao', 'oleo', 'creme de leite'] },
  { id: 3, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Salada', name: 'Arroz marroquino', key: '3', ingredients: ['A', 'B', 'C'] },
  { id: 4, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Sobremesa', name: 'Bolo', key: '4', ingredients: ['farinha', 'açucar', 'etc'] },
  { id: 5, easyOfPrepare: 'facil', author: 'www.', category: 'Doce', type: 'Bolo', name: 'doce de leite', key: '5', ingredients: ['AA', 'BB', 'CC'] },
];
const userInput = ['sal', 'ovo', 'mantega', 'maionese', 'farinha'];

const result = recipes.filter(recipe => userInput.every(i => recipe.ingredients.includes(i)));


console.log(result.length); // 1
 类似资料:
  • 问题内容: 我有两个这样的结果集: 我需要的最终结果是这些数组之间的差异–最终结果应如下所示: 是否可以在JavaScript中执行类似的操作? 问题答案: 仅使用本机JS,类似的方法将起作用:

  • 我的应用程序中有三个片段,其中需要传递和接收数据。我应该如何进行他们之间的沟通。我试图参考许多网站,但没有解决方案。 请给我推荐一些好的链接。 提前感谢。

  • 我想按时间对两个json数组stream和stream 1进行排序。 每个数组中时间最近的记录将排在最前面。时间的格式类似于分钟前、天前的值 这是我到目前为止的代码: 我需要对数组进行排序,首先显示每个数组中最近的项目,然后是下一个最近的项目,依此类推。

  • 如何过滤下面两个个不同的对象数组?然后展示它们之间不同数据 第一个对象数组: 第二个对象数组: 过滤后找出不同的数据祖晨更新的数组:

  • 以这个结构为例: 我想执行一个查询,首先验证用户()是否等于参考值。如果是,我希望它验证数组()是否包含某个标记。 这是我到目前为止能找到的最接近的东西: 此代码是为在Firebase上部署函数开发的,每当我拥有的某个集合上出现onWrite事件时,它就会触发。 此外,如果有人知道一些关于Firestore的复合查询的好例子,我将不胜感激。我已经阅读了文档,并看到了Firebase团队解释Fire

  • 我有三节课: 人类 父亲 孩子。 儿童阶级延伸父亲,父亲延伸人类。 我已经创建了每个类的一些实例并将它们存储到ArrayList中。 现在,我想编写一个方法来检查对象father1是否与对象child1地址字段(类Father和Child的实例)具有相同的字段地址(例如:“21 str Goodwin”),并将此方法提供给我的ArrayList,如果发现任何结果,则打印。 我怎么能这样呢? 为了更