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

138
Vistas
loop a var key in a res.json response

so this is the portion of my code :

var roomid= roomIds[i] 
const Availabilitydata = await AvailResponse.json();
    availableroomsArray.push(Availabilitydata);
    
        app.get("/api/availability", (req, res) => {
            res.json({ indicateur: availableroomsArray});});
    }

so the variable indicateur looping 3 rooms Ids in an array so when you visit /api/availability it will print the availibility of the 3 rooms in 2022-03-20 this way

{"indicateur":[{"20220320":{"i":1,"p1":"74.00","p2":"78.00","p3":"70.3000","p4":"122.4600"},"20220320"{"i":0,"p1":"74.00","p2":"77.00","p3":"70.3000","p4":"120.8900"},"20220320":{"i":0,"p1":"65.00","p2":"68.00","p3":"61.7500","p4":"106.7600"}}]}

and it is working fine .

What i am trying to achieve is displaying the roomId of each room in front of its availability like this :

{"1212":[{"20220320":{"i":1,"p1":"74.00","p2":"78.00","p3":"70.3000","p4":"122.4600"}]},
{"1213":[{"20220320"{"i":0,"p1":"74.00","p2":"77.00","p3":"70.3000","p4":"120.8900"}]},
{"1214":[{""20220320":{"i":0,"p1":"65.00","p2":"68.00","p3":"61.7500","p4":"106.7600"}}]}

I tried to replace indicateur in res.json({ indicateur: availableroomsArray});}); by roomid but it will be printed as it is

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The solution was to create an object var obj = {}; to store (key,value) pairs

   obj[roomIds[i]] = Availabilitydata; // key,value pairs object
    availableroomsArray.push(obj);

        app.get("/api/availability", (req, res) => {
            res.json({ availability : availableroomsArray});});
    }
about 4 years ago · Juan Pablo Isaza 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