0

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
2
  • your current result is a perfect array. I have a feeling that you're complicating the architecture by changing results into a abnormal format. I don't know about your requirement. Just my feeling Commented Oct 15, 2021 at 15:36
  • Ya....but I am creating a chatbot dialog flow where I need to pass as a variable so that the results are displayed to the user.......Is it possible to have a function that returns all these values? Commented Oct 15, 2021 at 15:59

2 Answers 2

1
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'
    }];

  let newresult = '';
  for(let i =0;i<result.length;i++) {                               
      var hotelname=result[i].HotelName;
      var city=result[i].City;
      var price=result[i].Price;  
      
      
      newresult+= `${i})\n`;
      newresult+= `hotelname: ${hotelname}\n`;
      newresult+= `city: ${city}\n`;
      newresult+= `price: ${price}\n`;
  }


  console.log(newresult);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!.this worked.Could you please add an y documentations to refer and know more about these basics?
1

You need to create an empty array outside of your loop and then push formatted strings into that array.

You can find documentation about the javascript features i used in my answer here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Something like 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'
      }
];

let hotels = [];

for(let i =0;i<result.length;i++) {                               
    let hotel = (
        `
      ${i})
      hotelname: ${result[i].HotelName}
      city: ${result[i].City}
      price: ${result[i].Price}
     `
    );
    hotels.push(hotel);
}

hotels.forEach(hotel => console.log(hotel));

An easier way would be to use the Array.map() function to achieve the same result without having to use a for loop.

Like 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'
  }
];
    

let formattedHostelString = result.map((hotel, index) => {
    return (
    `
      ${index})
      hostelname: ${hotel.HotelName}
      city: ${hotel.City}
      price: ${hotel.Price}
    `
  )
}).forEach(hotel => console.log(hotel));

5 Comments

Thanks for the code....But this displays in array format again...I need the expected response as shown in above format
Ah, you need a string in that format?
Yes...It would be really helpful if you can provide any documentations related to these.......I am currently referring all possible docs to know the basics.....but I am not able to get a hold on these basics...
Ok, i updated my answer and added links to documentation explaining the things i used here
Thanks so much..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.