Describing my issue, I am using a for loop to extract elements from an array and assign them to a JSON value. It looks something like this:
hotel={ rooms: 2, price: [ 100, 200 ], occupation: [ '1 child', '1 adult' ]
I aim to push this data into an array of JSONs:
hotels = [ { rooms:1, price: 100, occupation: '1 child' },... ]
This is what I have tried:
var json = { rooms : 1, price:null, occupation:null }
for (let i=0 ; i < hotel.rooms ; i++){
json.price = hotel.price[i]
json.occupation = hotel.occupation[i]
this.hotels.push(json)
}
However, the array hotels always ends up with the last values from the loop. I attempted handling this using try {throw i} catch(ii) {...}, but it did not work as expected.