ohdang888 Posted December 2, 2009 Share Posted December 2, 2009 var obj = {2:{"name":"Test for number 2", "other":"yup"}, 4:{"name":"What i want!", "other":"yup"} }; var test = 4; alert(obj.test.name);// Would say "What i want" var test = 2; alert(obj.test.name);// Would say "test for number 2" the variable "test" differs, so the path the values i'm looking for is differnt, how is this achieved? Thanks! Link to comment https://forums.phpfreaks.com/topic/183678-how-to-define-a-path-when-grabbing-var-from-json/ Share on other sites More sharing options...
gizmola Posted December 2, 2009 Share Posted December 2, 2009 You've created an array, so you simply reference it as an array element. var obj = {2:{"name":"Test for number 2", "other":"yup"}, 4:{"name":"What i want!", "other":"yup"} }; var test = 4; alert(obj[test].name);// Would say "What i want" var test = 2; alert(obj[test].name);// Would say "test for number 2" Link to comment https://forums.phpfreaks.com/topic/183678-how-to-define-a-path-when-grabbing-var-from-json/#findComment-969478 Share on other sites More sharing options...
rajivgonsalves Posted December 2, 2009 Share Posted December 2, 2009 this code should work.. var obj = {2:{"name":"Test for number 2", "other":"yup"}, 4:{"name":"What i want!", "other":"yup"} }; for (key in obj) { alert(obj[key].name);// Would say "What i want" } Link to comment https://forums.phpfreaks.com/topic/183678-how-to-define-a-path-when-grabbing-var-from-json/#findComment-969479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.