-2

My problem is:

Works:

console.log(variable[apple]);

variable: {
  apple : "xxx"
}

Not working:

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

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

element is not undefined. Any idea?

Tom Anderson
  • 440
  • 3
  • 10
HowToTellAChild
  • 623
  • 1
  • 9
  • 21
  • This `req.body` should be syntax error. Also `[apple]` should be in quotes – Rajesh Feb 05 '18 at 10:10
  • _"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve/)."_ – Andreas Feb 05 '18 at 10:11
  • this req.body is just an example... but okay, I change the variable names – HowToTellAChild Feb 05 '18 at 10:11
  • `console.log(variable.element.apple);` – gurvinder372 Feb 05 '18 at 10:13
  • @HowToTellForAChild Use **@** to tag a user – Rajesh Feb 05 '18 at 10:13
  • what about tell us your complete code,code above get errors – xianshenglu Feb 05 '18 at 10:13
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – str Feb 05 '18 at 10:14
  • Dear Andreas! My desired behaviour is to wok this statement: console.log(variable.element[apple]); My specific problem is: This statement above is not working! You don't help in my problem, if you just insert quotations. – HowToTellAChild Feb 05 '18 at 10:16
  • 1
    And your "question" doesn't help us to help you, hence the link on how to create [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve/) – Andreas Feb 05 '18 at 10:22
  • @Andreas if you want to really help me, or to others, then ask, if you don't understand something. – HowToTellAChild Feb 05 '18 at 10:42

1 Answers1

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"
  }
}

But there's no variable in your code. Read up about labeled statements

You probably mean

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);
Thomas
  • 11,958
  • 1
  • 14
  • 23