javascript - How do I access an objects key in bracket notation? -
i can't seem figure out how access spearguns object using bracket notation. i'm trying access "heft" key. console log say's "undefined". great, thanks.
var rockspearguns = { sharpshooter: {barbs: 2, weight: 10, heft: "overhand"}, pokepistol: {barbs: 4, weight: 8, heft: "shoulder"}, javelinjet: {barbs: 4, weight: 12, heft: "waist"}, firefork: {barbs: 6, weight: 8, heft: "overhand"}, "the impaler": {barbs: 1, weight: 30, heft: "chest"} }; function listguns(guns) { (var speargun in guns) { // modify log message here console.log("behold! " + speargun + ", " + this["heft"] + " heft!"); } } listguns(rockspearguns);
you need reference gun property name this:
var rockspearguns = { sharpshooter: {barbs: 2, weight: 10, heft: "overhand"}, pokepistol: {barbs: 4, weight: 8, heft: "shoulder"}, javelinjet: {barbs: 4, weight: 12, heft: "waist"}, firefork: {barbs: 6, weight: 8, heft: "overhand"}, "the impaler": {barbs: 1, weight: 30, heft: "chest"} }; function listguns(guns) { (var speargun in guns) { // modify log message here var gun = guns[speargun]; console.log("behold! " + speargun + ", " + gun["heft"] + " heft!"); } }
Comments
Post a Comment