3

Currently I have my scope set out in the following format.

   $scope.test = [
    {
        group: 'Group1',
        groupID:0,
        items: [
            {id: 0, description: 'Item 1', error: true, groupID:0},
            {id: 1, description: 'Item 2', error: true, groupID:0},
            {id: 2, description: 'Item 3', error: true, groupID:0},
            {id: 3, description: 'Item 4', error: true, groupID:0}
        ]
    },
    {
        group: 'Group2',
        groupID:1,
        items: [
            {id: 0, description: 'Item 1', error: true, groupID:1},
            {id: 1, description: 'Item 2', error: true, groupID:1},
            {id: 2, description: 'Item 3', error: true, groupID:1},
            {id: 3, description: 'Item 4', error: true, groupID:1}
        ]        
    },
    {
        group: 'Group3',
        groupID:2,
        items: [
            {id: 0, description: 'Item 1', error: true, groupID:2},
            {id: 1, description: 'Item 2', error: true, groupID:2},
            {id: 2, description: 'Item 3', error: true, groupID:2},
            {id: 3, description: 'Item 4', error: true, groupID:2}
        ]        
    }   

What I want is too loop through all the item objects in each parent object inside the controller and get a total of all items which the value 'error' is equal to true and alert this total.

Currently inside the controller I can only loop through the 1st level objects in the array, so can only access 'group' and 'groupID'.

2 Answers 2

4
function myCtrl($scope) {
    $scope.test = [{
        group: 'Group1',
        groupID: 0,
        items: [{
            id: 0,
            description: 'Item 1',
            error: true,
            groupID: 0
        }, {
            id: 1,
            description: 'Item 2',
            error: true,
            groupID: 0
        }, {
            id: 2,
            description: 'Item 3',
            error: true,
            groupID: 0
        }, {
            id: 3,
            description: 'Item 4',
            error: true,
            groupID: 0
        }]
    }, {
        group: 'Group2',
        groupID: 1,
        items: [{
            id: 0,
            description: 'Item 1',
            error: true,
            groupID: 1
        }, {
            id: 1,
            description: 'Item 2',
            error: true,
            groupID: 1
        }, {
            id: 2,
            description: 'Item 3',
            error: true,
            groupID: 1
        }, {
            id: 3,
            description: 'Item 4',
            error: true,
            groupID: 1
        }]
    }, {
        group: 'Group3',
        groupID: 2,
        items: [{
            id: 0,
            description: 'Item 1',
            error: true,
            groupID: 2
        }, {
            id: 1,
            description: 'Item 2',
            error: true,
            groupID: 2
        }, {
            id: 2,
            description: 'Item 3',
            error: true,
            groupID: 2
        }, {
            id: 3,
            description: 'Item 4',
            error: false,
            groupID: 2
        }]
    }];
    $scope.errors = [];


    function innerLoop(obj) {
        return function (items) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].error) {
                    $scope.errors.push(items[i].error);
                }
            }
        }(obj.items);
    }

    function loop(obj) {
        for (var i = 0; i < obj.length; i++) {
            innerLoop(obj[i]);
        }
        return $scope.errors;
    }

    loop($scope.test);
    alert($scope.errors.length);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use nested ng-repeat in view

see example

function myCtrl($scope){
$scope.multi = [1,2,[3,4,5]] ;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div  ng-app ng-controller="myCtrl">
<div ng-repeat='i in multi'>
  <span ng-hide="i.length>1">{{i}}</span>
  <div ng-repeat='i2 in i'>
  {{i2}}
  </div>
  
  </div>

2 Comments

Thanks for the answer, but I want one value returned which is the total, in my example I would expect 12 as 12 'error' values are true.
you dont want ui? just js?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.