@KnyazT

Как обратиться к элементу обьекту?

Весь код находится в переменной weather чтобы вызвать температуру только по одному городу (бернсвилу) я прописываю weather[0].location.name и weather[0].location.temperature. Вопрос в том что по всем городам одинаковое название обьектов location и temperature, подскажите как обратиться к конкретному городу? я ток учусь и чет не пойму как в этом случае прописывать
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"
      }
    ]
  }
]
  • Вопрос задан
  • 21 просмотр
Пригласить эксперта
Ответы на вопрос 1
YuriyVorobyov1333
@YuriyVorobyov1333
Software Developer
Преобразуйте ваш массив в объект, где ключом к данным будет название города, и тогда в дальнейшем, вам из кода будет легче получать данные из такой структуры, либо вам придется перебирать массив постоянно в поисках конкретного города и данных о нем
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы