0

So I am trying to access a data in an object, but I'm not sure how to do it. I tried with

myObj.weather["humidity"];

and

myObj.weather[1];

I am tring to acces for example the humidity from the json "82" but i getting "undefined"

<!DOCTYPE html>
<html>
<body>

<h2>Creating an Object from a Object Literal</h2>
<p id="demo"></p>

<script>
const myObj = {"code":200,"weather":[{"feels_like":21.48,"humidity":82,"pressure":1011,"temp":21.17,"temp_max":23.47,"temp_min":19.55}]};
document.getElementById("demo").innerHTML = myObj.weather["humidity"];
</script>

</body>
</html>
Daniel IG
  • 31
  • 6
  • That's not [JSON](https://www.json.org/json-en.html) -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Jun 28 '21 at 09:01
  • 2
    _"Describe the problem. **"It doesn't work" isn't descriptive enough** to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question."_ (Source: [mcve]) – Andreas Jun 28 '21 at 09:01
  • 1
    `myObj.weather` is an array – Andreas Jun 28 '21 at 09:02
  • 1
    `myObj["weather"][0]["humidity"]` – ahsan Jun 28 '21 at 09:03
  • 1
    Thank you both. The answer is myObj["weather"][0]["humidity"] thank you @AhsanKhan :D – Daniel IG Jun 28 '21 at 09:11

0 Answers0