Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Filter array with objects based on another array

How do I filter such array:

const recipes = [
{
    "id": 1,
    "name": "Boiled Egg",
    "ingredients": [
        "egg",
        "water"
    ],
},
{
    "id": 2,
    "name": "Pancakes",
    "ingredients": [
        "egg",
        "milk",
        "flour"
    ],
},
{
    "id": 3,
    "name": "Bread",
    "ingredients": [
        "flour",
        "water",
        "salt"

    ],
},
]

based on elements in such array

const selectedIngredients = ["milk", "salt"]

I played with combination of array.filter, array.some as show in Check if an array contains any element of another array in JavaScript but can't get it working properly

I want to get recipes with id 2 and 3 as a result

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can set the condition of the filter to whether selectedIngredients includes an item that is included in the item's ingredients property:

const recipes=[{id:1,name:"Boiled Egg",ingredients:["egg","water"]},{id:2,name:"Pancakes",ingredients:["egg","milk","flour"]},{id:3,name:"Bread",ingredients:["flour","water","salt"]}];

const selectedIngredients = ["milk", "salt"]

const result = !selectedIngredients.length ? [...recipes] : recipes.filter(e => selectedIngredients.some(f => e.ingredients.includes(f)))

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const recipes = [
{
    "id": 1,
    "name": "Boiled Egg",
    "ingredients": [
        "egg",
        "water"
    ],
},
{
    "id": 2,
    "name": "Pancakes",
    "ingredients": [
        "egg",
        "milk",
        "flour"
    ],
},
{
    "id": 3,
    "name": "Bread",
    "ingredients": [
        "flour",
        "water",
        "salt"
    ],
},
]

const selectedIngredients = ["flour", "water"]

const selectAny = (list, filter) =>
  list.filter(e => e.ingredients.some(i => filter.includes(i)));

const selectAll = (list, filter) =>
  list.filter(e => filter.every(i => e.ingredients.includes(i)));

console.log('any', selectAny(recipes, selectedIngredients));
console.log('all', selectAll(recipes, selectedIngredients));

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would do it like this

const recipes = [
    {
        id: 1,
        name: 'Boiled Egg',
        ingredients: ['egg', 'water'],
    },
    {
        id: 2,
        name: 'Pancakes',
        ingredients: ['egg', 'milk', 'flour'],
    },
    {
        id: 3,
        name: 'Bread',
        ingredients: ['flour', 'water', 'salt'],
    },
];

//

const doRebuild = (selectedIngredients)=>{
    const build = [];
    for(const two of selectedIngredients){
        for(const one of recipes){
            if(one.ingredients.some((a)=>two === a))build.push(one);
        }
    }
    return build.length > 0 ? build : ['No recipes'];
};

//

const content1 = doRebuild(['milk2', 'salt2']);
const content2 = doRebuild(['milk', 'salt']);

console.log(content1);
console.log(content2);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda