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

115
Views
devolver un objeto JSON de una solicitud GET

Estoy usando Node y Express para practicar la escritura de rutas y me dieron el siguiente ejercicio. ¿Alguien puede ayudar con la respuesta a mi solicitud GET? Dados los siguientes datos JSON:

 { "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" ] } ] }

Se supone que debo hacer una solicitud GET y devolver lo siguiente:

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

Esto es lo que tengo hasta ahora:

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

que me esta dando:

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

¿Cómo lo obtengo en un objeto JSON con la clave "recipeNames" y valoro la matriz?

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

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 Report

0

Puedes hacerlo de la siguiente manera:

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