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

132
Views
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 answers
Answer question

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