0

I'm calling a json string that has white spaces in the its identifiers which is causing my code to break. I'd like to call the first name and last name

var names = jQuery.parseJSON('[{"F name":"Chris1","Lname":"Test1"},
{"F name":"Chirs2 ","Lname":"Test2"},{"F name":"Chris3","Lname":"Test3"}]');

//This outputs **First Name: undefined** 
document.getElementById("demo").innerHTML=('First Name: '+names[1].Fname+'\n 
\n <br/>Last Name: '+names[1].Lname+'');

//This outputs nothing
document.getElementById("demo").innerHTML=('First Name: '+names[1].["F name"]+
'\n \n <br/>Last Name: '+names[1].Lname+'');

I know the naming sucks, and it should not have a space like "F name" but its not my original data so I have to use the naming convention. Do you know the correct syntax to display the first name in this array?

Thanks for your help!

1

1 Answer 1

3

It should be this:

names[1]["F name"]

Without the .

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

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.