0

I have accessed others parameters from an API using e.g:

dayTemp={`${Math.floor(weather?.main?.temp_max - 273)}°C`}

However when I'm trying to access rain:

rain={`${weather?.rain?.1h}`}

I get an error:

An identifier or keyword cannot immediately follow a numeric literal.

How can I access weather.rain.1h?

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 1
    Does `weather?.rain?["1h"]` work? See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Identifier_after_number – lux Mar 18 '22 at 17:31
  • Yeah square brackets property access notation is the way to go. You can even put variables in there like `weather.rain[\`${Math.random()}\`]` – windowsill Mar 18 '22 at 17:33
  • 1
    This worked: weather?.rain?.["1h"]. Thanks @lux. – IliassElyaa Mar 18 '22 at 17:47
  • Please look at tags before you use them. `API` tag explicitly says "DO NOT USE". Also, this has nothing to do with APIs and everything to do with JavaScript objects, property names, an how to access them – crashmstr Mar 31 '22 at 14:24
  • 1
    Duplicate: [Javascript: access an object property whose name starts with a number](https://stackoverflow.com/questions/16083883/javascript-access-an-object-property-whose-name-starts-with-a-number) – slhck Mar 31 '22 at 14:27

0 Answers0