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

83
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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