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

114
Visualizações
return a JSON object from a GET request

I am using Node and Express to practice writing routes and was given the following exercise. Can anyone help with the response to my GET request? Given the follow JSON data:

{
  "recipes": [
    {
      "name": "scrambledEggs",
      "ingredients": [
        "1 tsp oil",
        "2 eggs",
        "salt"
      ],
      "instructions": [
        "Beat eggs with salt",
        "Heat oil in pan",
        "Add eggs to pan when hot",
        "Gather eggs into curds, remove when cooked",
        "Salt to taste and enjoy"
      ]
    },
    {
      "name": "garlicPasta",
      "ingredients": [
        "500mL water",
        "100g spaghetti",
        "25mL olive oil",
        "4 cloves garlic",
        "Salt"
      ],
      "instructions": [
        "Heat garlic in olive oil",
        "Boil water in pot",
        "Add pasta to boiling water",
        "Remove pasta from water and mix with garlic olive oil",
        "Salt to taste and enjoy"
      ]
    },
    {
      "name": "chai",
      "ingredients": [
        "400mL water",
        "100mL milk",
        "5g chai masala",
        "2 tea bags or 20 g loose tea leaves"
      ],
      "instructions": [
        "Heat water until 80 C",
        "Add milk, heat until 80 C",
        "Add tea leaves/tea bags, chai masala; mix and steep for 3-4 minutes",
        "Remove mixture from heat; strain and enjoy"
      ]
    }
  ]
}

I am supposed to make a GET request and return the following:

{
    "recipeNames":
        [
            "scrambledEggs",
            "garlicPasta",
            "chai"
        ]
}

This is what I have so far:

app.get('/recipes', (req, res) => {
  const recipes = data.recipes;
  const recipeNames = {}
  const arr = []
  for(let i = 0; i < recipes.length; i++){
    let recipe = recipes[i]
    let recipeName = recipe.name
 arr.push(recipeName)
      //recipeNames[arr] = [recipeName]

 }

  res.status(200).send(arr)
})

which is giving me :

{
  [
     "scrambledEggs",
     "garlicPasta",
     "chai"
  ]
}

How do I get it in a JSON object with key "recipeNames" and value the array?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

app.get('/recipes', (req, res) => {
  const recipes = data.recipes;
  const recipeNames = recipes.recipeNames.map(r => r.name)

  res.status(200).send({recipeNames})
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You can make it as bellow:

app.get('/recipes', (req, res) => {
    const recipes = data.recipes;
    const recipeNames = {}
    const arr = []

    for(let i = 0; i < recipes.length; i++){
        let recipe = recipes[i]
        let recipeName = recipe.name
        arr.push(recipeName)
        //recipeNames[arr] = [recipeName]

    }

    let forSend = {"recipeNames": arr}

    res.status(200).send(forSend)
})       
about 4 years ago · Juan Pablo Isaza 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