0

I'm trying out the Zomato api to pull out some data. However, I'm stuck in reviews. I can't seem to get the reviews because I end up getting [Object Object] as the value for the text. If anyone knows how to get the reviews to show up as text let me know.

enter image description here

link to actual code: https://jsfiddle.net/fxrzm/nkLrh8fg/4/

const {thumb:img,name,location:{address},all_reviews:{reviews},user_rating:{aggregate_rating},cuisines,average_cost_for_two:cost} = restaurant.restaurant;

// I was able to pull out the other data in const other than reviews as you can see.

Kainix
  • 1,186
  • 3
  • 21
  • 33

2 Answers2

1

Chances are the reviews are json objects and you'll need to pull out the relevant bits from the reviews to be displayed. See the following as an example:

const reviews = [
  { html: '<p>great</p>' },
  { html: '<p>awesome</p>' },
  { html: '<p>meh</p>' },
];

const div1 = document.getElementById('straight');
div1.innerHTML = `${reviews}`;


const div2 = document.getElementById('mapped');
div2.innerHTML = `${reviews.map(r => r.html).join('')}`;
<div id="straight"></div>
<div id="mapped"></div>
Soc
  • 7,425
  • 4
  • 13
  • 30
0

In Javascript Arrays are objects. It's also possible that reviews is a regular {} object. [Object Object] is what I would expect from a console log because console logs don't iterate through objects and display all their values if they are nested like that. You should console log restaurant.restaurant.reviews and get the structure. That way you will know more about the structure. I always try and go one level deeper until the structure makes sense.

The good news is you probably have the data you want, you just can't see it.

Update: I re-read your question. If you are using es6 you might want to try the spread operator to copy the keys and values instead of nesting an array or object in the new object. all_reviews:{...reviews}.

banderson
  • 121
  • 6