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.
asked Feb 6, 2017 at 2:13
MaxPower
8811 gold badge9 silver badges26 bronze badges
2 Answers 2
var object = ['{ "a": "foo", "b": "bar", "c": "baz" }'];
console.log(JSON.parse(object[0]).a);
answered Feb 6, 2017 at 2:18
rasmeister
2,0061 gold badge14 silver badges19 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
MaxPower
Thanks! That was exactly it
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
answered Feb 6, 2017 at 2:19
pryxen
4331 gold badge4 silver badges24 bronze badges
Comments
lang-js
JSON.parse(object[0]).