Home > Software engineering >  convert array of json to the expected result below nodejs
convert array of json to the expected result below nodejs

Time:10-16

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

CodePudding user response:

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);

CodePudding user response:

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));
  • Related