We help IT Professionals succeed at work.

angularjs foreach

Seven price
Seven price asked
on
I only want to ge the first value only. either break the loop once the value is found or another way to retrieve the first value only.
.success(function (data, status, headers, config) {
                        $scope.myData = data;
angular.forEach($scope.myData, function (item) {
                           // $scope.mypage = item.GroupID
                         
                               alert(item.GroupID);
                               return false;
                       
                       })
Comment
Watch Question

Hi,

If it is an array, why don't you just use the index?

Var firstItem = $scope.myData[0];

Open in new window


You may also use a limitTo filter. You would need to inject the $filter service on your controller.

Var firstItem = $filter('limitTo')($scope.myData,1);

Open in new window


The second option will return an array containing only one item.

Giannis
Seven priceFull Stack

Author

Commented:
alert(data[0].GroupID);
thanks