简体   繁体   English

如何访问括号符号中的对象键?

[英]How do I access an objects key in bracket notation?

I can't seem to figure out how to access the spearguns object using bracket notation. 我似乎无法弄清楚如何使用括号表示法访问spearguns对象。 I'm trying to access the "heft" key. 我正在尝试访问“ heft”键。 The console log say's "undefined". 控制台日志说“未定义”。 Any help would be 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) {
  for (var speargun in guns) {
    // modify the log message here
    console.log("Behold! " + speargun + ", with " + this["heft"] + " heft!");
  }
}

listGuns(rockSpearguns);

You need to get a reference to the gun from the property name like 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) {
    for (var speargun in guns) {
        // modify the log message here
        var gun = guns[speargun];
        console.log("Behold! " + speargun + ", with " + gun["heft"] + " heft!");
    }
}

Here is what I ended up using: 这是我最终使用的内容:

function listGuns(guns) {
  for (var speargun in guns) {
    // modify the log message here

    console.log("Behold! " + speargun + ", with " + guns[speargun]["heft"] + " heft!");
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用方括号表示法使用多个变量来调用嵌套对象? - How do I use Square Bracket Notation to use multiple variables to call nested objects? 如何用方括号表示法实现这个 curry function "add"? - How do I achieve this curry function "add" with square bracket notation? javascript 中的括号符号如何访问 object 的属性? - How bracket notation in javascript access the property of an object? 仅使用属性访问器(点符号或方括号符号),如何直接设置未定义的嵌套属性? - Using only property accessors (dot notation or bracket notation), how do I set undefined nested properties directly? 使用Java括号表示法来访问存储在另一个变量中的属性/键 - Javascript bracket notation to access a property/key stored in another variable 使用Lodash`_.get`使用括号表示法访问对象键 - Using Lodash `_.get` to access object key using bracket notation javascript点表示法在对象数组中括号表示法 - javascript dot notation to bracket notation in array of objects 如何使用JavaScript中的括号符号将函数附加到对象? - How to attach functions to objects using the bracket notation in JavaScript? 如何通过字符串作为括号表示法来获取javascript中的嵌套对象? - How to get nested objects in javascript by an string as a bracket notation? 如何使用括号符号在对象文字上创建嵌套属性? - How do I create nested properties on object literals using bracket notation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM