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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -