0

I have an array and want to access its data. However, I need to use a variable instead of the name to access the data.

For example;

My data:

$scope.myData = {

    "user": [
        {   child[{......}],
            ..........

        }
    ],"user2": [
        {   
            child[{......}],
             .........
        }
    ],...........

The following works

 console.log("lenght:"+$scope.myData.user[0].child.length);

but I want to use a variable instead of user[0], because it is dynamic, it changes every time.

Similar to

  var m=user;
  console.log("lenght:"+$scope.myData.m[0].child.length);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user4773604
  • 451
  • 3
  • 9
  • 16

1 Answers1

2

How about this?

   var m = 'user';
    console.log("lenght:"+ $scope.myData[m][0].child.length );
Eddie
  • 26,593
  • 6
  • 36
  • 58