15

Says I have var input = {'name':'john'}

I do input['name'] = 'James'

it become var input = {'name':'john'} but can I pass value with dot to access nested property?

Like

var input = {"name":"john","grades":"{english:"A","math":"C"}"}

I can't change the math value by doing input["grades.math"].

Rajesh
  • 24,354
  • 5
  • 48
  • 79
Alex Yong
  • 7,425
  • 8
  • 24
  • 41
  • 5
    `input["grades"]["math"]` – cartant Mar 07 '17 at 06:24
  • 1
    No. You can do `input["grades]["math"]"` You can have properties with special characters that cannot be defined using dot notation. So you will have to use bracket notation – Rajesh Mar 07 '17 at 06:25
  • Possible Duplicate: http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Rajesh Mar 07 '17 at 06:29

1 Answers1

22

You can access that value by these ways:

var input = {"name":"john","grades":{"english":"A","math":"C"}}

console.log(input["grades"]["math"]);
console.log(input.grades.math);
console.log(input["grades"].math);
console.log(input.grades["math"]);
Ngoan Tran
  • 1,507
  • 1
  • 13
  • 17