Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Question of $.getJSON (Brackets)

1) Create a variable named data. Assign it an empty JavaScript object. Hint: an object in JavaScript is created with a pair of braces like this: { }
2) Finally, to insert the temperature into the span, use jQuery's text() method. You'll need to pass the temperature into this method. The Open Weather Map API returns an object with the property main; that property holds another object with the property "temp". To access the temperature, use dot syntax, like this: weatherReport.main.temp

$(document).ready(function() {
  var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
  var data = {
    q : "Portland,OR",
    units : "metric"
  };
  function showWeather(weatherReport) {
    $('#temperature').text(weatherReport.main.temp);
  }
  $.getJSON(weatherAPI, data, showWeather);  
});

1) If var data is an javascript object, why use {} not [].
2) $('#temperature').text(weatherReport.main.temp); What is the object look like ?
[
ā€ƒ"main" : {
ā€ƒā€ƒ"temp" : ""
ā€ƒ}
]

2 Answers

1) objects use {} and arrays use []. But you can also have objects containing multiple arrays or arrays of multiple objects. This can be confusing at first but its valuable if understood. For this case var data is an object with 2 keys: q and units.

2) you could use the console.log method on the weatherReport and see exactly how the object returned looks like.

so if var data containing 2 objects it will look like ? is this correct ?

var data = [
  {
    q : "Portland,OR",
    units : "metric"
  },
  {
    A : "Portland,OR",
    units : "metric"
  }
]

Yes. You have the var data that stores an array containing 2 objects.

Thank you so much !