Javascript获取属性值

时间:2018-02-05 10:09:11

标签: javascript json node.js

我的问题是:

使用:

console.log(variable[apple]);

variable: {
  apple : "xxx"
}

不工作:

console.log(variable.element[apple]);

variable: {
  element: {
    apple: "xxx"
  }
}

element未定义。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

variable: {  //this is a code block labeled "variable"
  element: {  //this is a code block labeled "element"
    apple: "xxx"  //this is an expression "xxx" labeled as "apple"
  }
}

但是你的代码中没有变量。阅读labeled statements

你可能意味着

var variable = {  //now this is a variable containing an object
  element: {  //and these are properties of that object
    apple: "xxx"  
  }
}

console.log(variable.element.apple);