2

How do I print out the value of category.uri when the object is defined like this:

API

If i try using object[0].category.@uri I get the following error:

Uncaught SyntaxError: Unexpected token +

And if I try without the @-sign, I get:

Uncaught TypeError: Cannot read property 'uri' of undef

Basically: how do I read the value when its prepended with an at sign? What does the at sign mean aka why is it there in the first place? (In this case I do not control the REST-API I am working against)

subZero
  • 5,056
  • 6
  • 31
  • 51

2 Answers2

8

Javascript has two object access notations (called member operators): dot notation and square bracket notation. These are identical:

document.forms
document['forms']

You can always use square bracket notation, but you can't always use the dot notation. So if your property name does not conform to the rules for identifiers, you need to use the square bracket form.

object[0].category['@uri']

The rules are:

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). (MDN)

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
4

Like this:

object[0].category['@uri']
nick
  • 3,521
  • 3
  • 22
  • 32