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

82
Views
I am trying to make a function which will return an id from array of object

I want to write a function which will return id from array of object but when i call that function it returns me what I pass.

export function getRecipeByID(requestId) {
  recipes.find(function (recipe) {
    return recipe.id === requestId;
  });
  return requestId;
  
} 

for example I call function

getRecipeByID(1)

and it returns 1.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

I guess what you want to write is this:

export function getRecipeByID(requestId) {
  return recipes.find(function (recipe) {
    return recipe.id === requestId;
  });
  
}

Notice that it doesn't return requestId but the result of recipes.find()

about 4 years ago · Santiago Gelvez Report

0

you return requestId after using find which cause issue

     const recipes = [
      {
        id: 2,
        name: 'pasta'
      },
      {
        id: 3,
        name: 'sandwich'
      },
      {
        id: 4,
        name: 'pizza'
      }
    ]
     
    function getRecipeById(requestId) {
       const findRecipe = recipes.find(function (recipe) {
        return recipe.id === requestId;
      });
      return findRecipe;
    }
    console.log(getRecipeById(2)); // it will return{ id:2, name:"pasta" }

about 4 years ago · Santiago Gelvez Report

0

That's because you return requestId in your method while what you want is to return the result of recipes.find...

export function getRecipeByID(requestId) {
  return recipes.find(function (recipe) {
    return recipe.id === requestId;
  });  
} 
about 4 years ago · Santiago Gelvez 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!