17

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"].

3
  • 5
    input["grades"]["math"] Commented Mar 7, 2017 at 6: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 Commented Mar 7, 2017 at 6:25
  • Possible Duplicate: stackoverflow.com/questions/11922383/… Commented Mar 7, 2017 at 6:29

1 Answer 1

24

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"]);

Sign up to request clarification or add additional context in comments.

1 Comment

Thus, to clarify, it's not possible in the way he's after.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.