Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
How to match array with onther array inside an object ? (Js)

begginer developer (JS,ReactNative) . i want to match an array of ingridients thats passed to the function as a parameter to extricate the recipies thats match . iv'e tryied diffrent functions like map and filter but without succsess . i need to use PureJs function .

Example :

             Ingridients = ['eggs', 'lemon']   ----->   (Passed as a parameter)

             Recipies = [
                          {
                            _id:1,
                            _recipieName:"Eggs and Lemon",
                            _recipieIngridients:["eggs","lemon"]
                          },
                          {
                            _id:2,
                            _recipieName:"Onther Recipie",
                            _recipieIngridients:["water","bread","blablabla"]
                           }
                         ]

                i need that result-->

                     NewArray = [
                          {
                            _id:1,
                            _recipieName:"Eggs and Lemon",
                            _recipieIngridients:["eggs","lemon"]
                          }

Thx.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This can achieved by doing the following

Exact match:

const matchRecipe = (ingredients) => 
   Recipies
      .filter(r => r._recipieIngridients.length === ingredients.length && r._recipieIngridients.every((ingredient, index) => ingredient === ingredients[index]))

Here, I'm filtering the recipies using filter, and as a predicate, I'm using every to check that each element inside the _recipieIngridients matches each element inside ingredients by using the index that is taken as a second argument from the every function

If you don't need it to match the position of the element, you can sort both ingredients so that they should all match whatever the previous order is

A more simplified version without using nested prototype functions:

const matchRecipe = (ingredients) => Recipes.filter(recipe => {
  if (ingredients.length !== recipe._recipieIngridients) return false
  // Sort both if needed
  for (let i = 0; i < recipe._recipieIngridients.length; i++) {
    if (recipe._recipieIngridients[i] !== ingredients[i]) return false
  }
  return false
})

Hope you find this helpful

Edit (1) For your comment, if you want partial matching, you can do the following

const matchRecipe = (ingredients) => 
   Recipies
      .filter(r => r._recipieIngridients.length === ingredients.length && r._recipieIngridients.some((ingredient) => ingredients.include(ingredient)))

The prototype function some will return true once any of the elements match any of the ingredients by using the include function

about 4 years ago · Juan Pablo Isaza Report

0

You can put the ingredients into Set to compare them efficiently, for example

const recipies = [
  {
    _id:1,
    _recipieName:"Eggs and Lemon",
    _recipieIngridients:["eggs","lemon"]
  },
  {
    _id:2,
    _recipieName:"Onther Recipie",
    _recipieIngridients:["water","bread","blablabla"]
  },
  {
    _id:3,
    _recipieName:"Eggs with Cheese Recipie",
    _recipieIngridients:["eggs", "cheese"]
  } 
];

function filterByIngridients(recipies, ingridients, howMatch = 'every') {
  const set = new Set(ingridients);
  return recipies.filter((r) => {
    return r._recipieIngridients[howMatch]((r) => set.has(r));
  });
}

Now you can match every ingredient

let output = filterByIngridients(recipies, ['eggs', 'lemon']);
console.log(output);

Output:

[
  {
    _id: 1,
    _recipieName: 'Eggs and Lemon',
    _recipieIngridients: [ 'eggs', 'lemon' ]
  }
]

Or some ingredients

output = filterByIngridients(recipies, ['eggs', 'lemon'], 'some');
console.log(output);

Output:

[
  {
    _id: 1,
    _recipieName: 'Eggs and Lemon',
    _recipieIngridients: [ 'eggs', 'lemon' ]
  },
  {
    _id: 2,
    _recipieName: 'Eggs with Cheese Recipie',
    _recipieIngridients: [ 'eggs', 'cheese' ]
  }
]
about 4 years ago · Juan Pablo Isaza Report

0

You should try something like that:

const availableReciepes = Recipies.reduce((result, reciepe)=>{
    let hasAllIngredients = Ingridients.reduce((carry, ingredient)=>{
        if(!reciepe.ingredient) 
            return false;
        return carry;
    }, true);

    if(hasAllIngredients){
        result.push(reciepe);
    }

    return result;
}, []);

Basically, for every recipe, check if you have all the ingredients, then adds it to the reduce carry.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!