I am a beginner to Nodejs and I am not sure if I have formulated the question in a right manner.
I have a result variable which contains the values as shown below:
var result=[
{
Price: 100,
Availability: 'Yes',
City: 'Mumbai',
HotelName: 'GrandHyatt'
},
{
HotelName: 'Leelaplace',
Price: 110,
City: 'Banglore',
Availability: 'Yes'
},
{
HotelName: 'OberaiHotel',
City: 'Mumbai',
Availability: 'Yes',
Price: 150
},
{
HotelName: 'Taj Hotel',
Availability: 'yes',
Price: 180,
City: 'Mumbai'
}
];
I want the response in below format:
0)
hotelname: GrandHyatt
city:Mumbai
price:100
1)
hotelname: Leelaplace
city:Banglore
price:110
2)
hotelname: OberaiHotel
city:Mumbai
price:150
3)
hotelname: Taj Hotel
city:Mumbai
price:180
I tried this:
var result=[
{
Price: 100,
Availability: 'Yes',
City: 'Mumbai',
HotelName: 'GrandHyatt'
},
{
HotelName: 'Leelaplace',
Price: 110,
City: 'Banglore',
Availability: 'Yes'
},
{
HotelName: 'OberaiHotel',
City: 'Mumbai',
Availability: 'Yes',
Price: 150
},
{
HotelName: 'Taj Hotel',
Availability: 'yes',
Price: 180,
City: 'Mumbai'
}
];
for(let i =0;i<result.length;i++) {
var hotelname=result[i].HotelName;
var city=result[i].City;
var price=result[i].Price;
console.log(i+')')
console.log("hotelname: "+ hotelname)
console.log("city:" + city)
console.log("price:"+price)
}
But ,I want to store all the expected response in a variable and access this outside the for loop.
For example,
console.log("newresult",newresult);//this all below should get printed
newresult
0)
hotelname: GrandHyatt
city:Mumbai
price:100
1)
hotelname: Leelaplace
city:Banglore
price:110
2)
hotelname: OberaiHotel
city:Mumbai
price:150
3)
hotelname: Taj Hotel
city:Mumbai
price:180