ganesh satpute ganesh satpute - 3 months ago 11
AngularJS Question

Access nested object with variable angularjs

obj {
val1 {
nestedval {}
}
val2 {
nestedval {}
}
}


I want to access nestedval and want val1 or val2 as a variable i am trying like

var getVal = obj.varible.nestedval


but some how it's not working for me can anyone please help me to achieve this using angularjs. Thanks for your valuable time Guys.

Answer

Providing that your object structure look like this (added : to make it valid):

obj = {
  val1: {
    nestedval: {}
  }
  val2: {
    nestedval: {}
  }
}

You can use bracket notation to refer property by variable name:

var variable = 'val2'
var getVal = obj[variable].nestedval