1

I have the following object

 var object =   ["{
 "a": "foo",
 "b": "bar",
 "c": "baz"
}
"]

I am interested in getting value a, but everything I have tried comes up undefined. I have tried object.a, object[0].a and object[a], I know it's something silly I am simply forgetting. Any help is greatly appreciated.

2
  • is there really a string in that array? Commented Feb 6, 2017 at 2:14
  • 2
    if so use JSON.parse(object[0]). Commented Feb 6, 2017 at 2:15

2 Answers 2

2

var object =   ['{  "a": "foo",  "b": "bar",  "c": "baz" }'];
console.log(JSON.parse(object[0]).a);

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

1 Comment

Thanks! That was exactly it
1

your array should look like this

var object =   [{
 "a": "foo",
 "b": "bar",
 "c": "baz"
}];

remove the double quotes so that you can access the object inside the array or else you can use the JSON.parse() just like @Daniel did

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.