0

I have the following object self.originalData on the console:

enter image description here

However, when I try to access to first object in the array of originalData,

self.originalData[0hcMSJXljH]

getting the following error

the Uncaught>Syntax Error: Unexpected token ILLEGAL

I could not able to figure out where I am doing wrong.

2

5 Answers 5

3

You can use:

self.originalData["0hcMSJXljH"]

instead. Object keys are strings so if you use the [] notation, then you have to put a string or a variable that contains a string inside the brackets.

Your particular case is a bit unusual because usually, you can use the dot notation as in obj.property, but because your key starts with a number, it is not a legal identifier to use with the dot notation (you can't do self.originalData.0hcMSJXljH). So, you are forced to use the bracket notation with that particular key.

Sign up to request clarification or add additional context in comments.

Comments

2

Try putting the key in quotes like this:

self.originalData['0hcMSJXljH']

Comments

1

You don't use quotes in your key, so it seems you are trying to use the variable identified by 0hcMSJXljH as the key. However, 0hcMSJXljH isn't a valid variable identifier, because it begins with a number, so your get an illegal-character error.

Simply use a string, not an identifier:

self.originalData["0hcMSJXljH"]

Comments

1

Have you tried

self.originalData["0hcMSJXljH"];

?

Otherwise:

self.originalData.0hcMSJXljH;

EDIT: last one not possible because the first char is a number, as explained to me

2 Comments

You can't do the second one. Identifiers used with the dot notation in Javascript cannot start with a number.
Yep, I noticed from your answers, my bad
1

You must use quotes:

self.originalData['0hcMSJXljH']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.