K
K
KnyazT2021-01-14 07:40:04
Arrays
KnyazT, 2021-01-14 07:40:04

How to refer to an object element?

All the code is in the weather variable to call the temperature for only one city (Burnsville) I write weather[0].location.name and weather[0].location.temperature. The question is that for all cities the same name of the location and temperature objects, tell me how to refer to a specific city? I'm learning current and even I don't understand how to prescribe in this case

spoiler
[
  {
    "location": {
      "name": "Burnsville, MN",
      "lat": "44.762",
      "long": "-93.276",
      "timezone": "-6",
      "alert": "",
      "degreetype": "C",
      "imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
    },
    "current": {
      "temperature": "1",
      "skycode": "29",
      "skytext": "Partly Cloudy",
      "date": "2021-01-13",
      "observationtime": "22:10:00",
      "observationpoint": "Burnsville, MN",
      "feelslike": "-1",
      "humidity": "87",
      "winddisplay": "5 km/h South",
      "day": "Wednesday",
      "shortday": "Wed",
      "windspeed": "5 km/h",
      "imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/29.gif"
    },
    "forecast": [
      {
        "low": "-3",
        "high": "3",
        "skycodeday": "31",
        "skytextday": "Mostly Clear",
        "date": "2021-01-12",
        "day": "Tuesday",
        "shortday": "Tue",
        "precip": ""
      },
      {
        "low": "-1",
        "high": "5",
        "skycodeday": "30",
        "skytextday": "Partly Sunny",
        "date": "2021-01-13",
        "day": "Wednesday",
        "shortday": "Wed",
        "precip": "0"
      },
      {
        "low": "0",
        "high": "1",
        "skycodeday": "16",
        "skytextday": "Snow",
        "date": "2021-01-14",
        "day": "Thursday",
        "shortday": "Thu",
        "precip": "100"
      },
      {
        "low": "-5",
        "high": "0",
        "skycodeday": "16",
        "skytextday": "Snow",
        "date": "2021-01-15",
        "day": "Friday",
        "shortday": "Fri",
        "precip": "100"
      },
      {
        "low": "-7",
        "high": "-2",
        "skycodeday": "26",
        "skytextday": "Cloudy",
        "date": "2021-01-16",
        "day": "Saturday",
        "shortday": "Sat",
        "precip": "100"
      }
    ]
  },
  {
    "location": {
      "name": "Minneapolis, MN",
      "lat": "44.977",
      "long": "-93.264",
      "timezone": "-6",
      "alert": "",
      "degreetype": "C",
      "imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
    },
    "current": {
      "temperature": "-3",
      "skycode": "31",
      "skytext": "Clear",
      "date": "2021-01-13",
      "observationtime": "22:10:00",
      "observationpoint": "Minneapolis, MN",
      "feelslike": "-3",
      "humidity": "87",
      "winddisplay": "0 km/h",
      "day": "Wednesday",
      "shortday": "Wed",
      "windspeed": "0 km/h",
      "imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif"
    },
    "forecast": [
      {
        "low": "-4",
        "high": "3",
        "skycodeday": "31",
        "skytextday": "Clear",
        "date": "2021-01-12",
        "day": "Tuesday",
        "shortday": "Tue",
        "precip": ""
      },
      {
        "low": "-1",
        "high": "5",
        "skycodeday": "30",
        "skytextday": "Partly Sunny",
        "date": "2021-01-13",
        "day": "Wednesday",
        "shortday": "Wed",
        "precip": "0"
      },
      {
        "low": "-1",
        "high": "1",
        "skycodeday": "16",
        "skytextday": "Snow",
        "date": "2021-01-14",
        "day": "Thursday",
        "shortday": "Thu",
        "precip": "90"
      },
      {
        "low": "-4",
        "high": "1",
        "skycodeday": "16",
        "skytextday": "Snow",
        "date": "2021-01-15",
        "day": "Friday",
        "shortday": "Fri",
        "precip": "100"
      },
      {
        "low": "-7",
        "high": "-3",
        "skycodeday": "26",
        "skytextday": "Cloudy",
        "date": "2021-01-16",
        "day": "Saturday",
        "shortday": "Sat",
        "precip": "100"
      }
    ]
  },
  {
    "location": {
      "name": "Moose Lake, MN",
      "lat": "46.454",
      "long": "-92.769",
      "timezone": "-6",
      "alert": "",
      "degreetype": "C",
      "imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
    },
    "current": {
      "temperature": "-2",
      "skycode": "31",
      "skytext": "Clear",
      "date": "2021-01-13",
      "observationtime": "22:10:00",
      "observationpoint": "Minneapolis, MN",
      "feelslike": "-2",
      "humidity": "92",
      "winddisplay": "0 km/h",
      "day": "Wednesday",
      "shortday": "Wed",
      "windspeed": "0 km/h",
      "imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/31.gif"
    },
    "forecast": [
      {
        "low": "-12",
        "high": "-1",
        "skycodeday": "29",
        "skytextday": "Partly Cloudy",
        "date": "2021-01-12",
        "day": "Tuesday",
        "shortday": "Tue",
        "precip": ""
      },
      {
        "low": "-3",
        "high": "2",
        "skycodeday": "30",
        "skytextday": "Partly Sunny",
        "date": "2021-01-13",
        "day": "Wednesday",
        "shortday": "Wed",
        "precip": "0"
      },
      {
        "low": "-1",
        "high": "0",
        "skycodeday": "16",
        "skytextday": "Snow",
        "date": "2021-01-14",
        "day": "Thursday",
        "shortday": "Thu",
        "precip": "100"
      },
      {
        "low": "-4",
        "high": "0",
        "skycodeday": "13",
        "skytextday": "Light Snow",
        "date": "2021-01-15",
        "day": "Friday",
        "shortday": "Fri",
        "precip": "100"
      },
      {
        "low": "-8",
        "high": "-2",
        "skycodeday": "26",
        "skytextday": "Cloudy",
        "date": "2021-01-16",
        "day": "Saturday",
        "shortday": "Sat",
        "precip": "100"
      }
    ]
  }
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2021-01-14
@YuriyVorobyov1333

Transform your array into an object where the name of the city will be the key to the data, and then in the future, it will be easier for you to get data from such a structure from the code, or you will have to iterate through the array constantly in search of a specific city and data about it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question