-3

After fetching a json object from an external API I get the following as an example:

id: 1
name: john
email: something@example.com
3a5411a124378534906a883a0c5ccda5724175eb: USA

So, in JavaScript I can easily access: object.id, object.name, etc.

However, object.3a5411a124378534906a883a0c5ccda5724175eb throws the error:

Identifier directly after number

How do deal with a situation like that? Or, in other words, how can I get the value of USA?

Flimzy
  • 68,325
  • 15
  • 126
  • 165
T. Short
  • 3,260
  • 11
  • 29
  • 1
    There's nothing "bad" about that ID. Just use `object["3a5411a124378534906a883a0c5ccda5724175eb"]` – Flimzy Aug 25 '18 at 11:02

2 Answers2

1

use

object["3a5411a124378534906a883a0c5ccda5724175eb"];
Kudratillo
  • 185
  • 10
1

Use the for-in loop on object to access all the properties of objects as follows event the properties like you mention,

var obj = {
id: 1,
name: 'john',
email: 'something@example.com',
'3a5411a124378534906a883a0c5ccda5724175eb': 'USA'
}
for(var prop in obj){
//do the stuff here what you want for each properties
    console.log(obj[prop]);
}
Naim Sulejmani
  • 1,102
  • 9
  • 9