Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access nested property using bracket notation [duplicate]

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

like image 685
Alex Yong Avatar asked Mar 07 '17 06:03

Alex Yong


1 Answers

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"]);
like image 151
Ngoan Tran Avatar answered Sep 21 '22 08:09

Ngoan Tran