Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda