btmach
btmach

Reputation: 375

Reaching a specific property of the data json object

I would like to reach a specific property of the data that I have returned from my service.

So basically I want to be able to somehow reach $scope.users.name, I know that the users are the objects in the array, but could I reach that specific property in any way? Hopefully the question is clear enough?

$scope.users = [];

    UserService.getAll().then(
        function (data) {
            $scope.users = data;
        }, function (err) {
            console.log(err);
        }
    );

Upvotes: 0

Views: 59

Answers (1)

ArslanW
ArslanW

Reputation: 353

I am assuming the data you receive is in the form of an array. If you know the index then you can do

$scope.users[2].name

Where 2 is the index of the object you want to know the name property of.

Or you can try a js function forEach

$scope.users.forEach(function (user) {
  console.log(user.name);
});

The function will iterate over all the objects and you can access their properties inside the callback which is passed in.

Hope that is what you're looking for.

Upvotes: 2

Related Questions