1

I have a JavaScript variable:

var field = "Name"; 

Now I have an Object:

var obj = {Name: Bhaskor,Surname: Sarmah};

Now I need to select the Name field of obj using my field variable value:

var name = obj.(field);

Is there any way to do this?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114

2 Answers2

3

You need to use bracket notation as the member operator to get a property using a variable

var name = obj[field];
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

You simply need to replace your brackets:

var name = obj[field];
baao
  • 71,625
  • 17
  • 143
  • 203