Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/React Native-如何在两个不同的对象数组之间进行交叉检查?_Javascript_Node.js_Reactjs_React Native - Fatal编程技术网

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

Javascript/React Native-如何在两个不同的对象数组之间进行交叉检查?,javascript,node.js,reactjs,react-native,Javascript,Node.js,Reactjs,React Native,我有一个应用程序,它将创建一个配方数据库,用户将在其中输入他拥有的配料,应用程序应返回用户已输入的此类组合的可用配方 一旦我有了包含许多键/值信息的配方对象: const [recipes, setRecipes] = useState([ { id: 1, easyOfPrepare: 'facil', author: 'www.', category: 'Salgada', type: 'Prato principal', name: 'pão de forma', key: '1

我有一个应用程序,它将创建一个配方数据库,用户将在其中输入他拥有的配料,应用程序应返回用户已输入的此类组合的可用配方

一旦我有了包含许多键/值信息的配方对象:

 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]);
}
一旦用户通知了预期成分,就会调用此附加成分

一旦用户输入了以下组合:“sal”、“ovo”、“mantega”、“maionese”、“farinha”,我如何根据配方筛选这些成分,以确定此组合中唯一可能的成分是第一种


谢谢

您可以使用
过滤器
包括
方法来实现这一点:

const matchedRecipes=recipes.filter(recipe=>{
用于(配料中的配料){
如果(!配方.配料.包括(配料)){
返回错误
}
}
返回真值
})

您可以使用
过滤器
包括
方法来实现这一点:

const matchedRecipes=recipes.filter(recipe=>{
用于(配料中的配料){
如果(!配方.配料.包括(配料)){
返回错误
}
}
返回真值
})

您可以根据
配方筛选出
成分

const filteredRecipes = recipes.filter(recipe => {
   return ingredients.every(ingredient => recipe.ingredients.includes(ingredient))
})

您可以根据
配方筛选出
成分

const filteredRecipes = recipes.filter(recipe => {
   return ingredients.every(ingredient => recipe.ingredients.includes(ingredient))
})

利用阵列的方法:

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

利用阵列的方法:

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