简体   繁体   中英

How to loop through array with arrays/objects and assign properties new values in Angular?

I have an array of arrays and objects. I have a function whereI want to assign a value to a property (so for example 'call':'' of $scope.companies[0].users becomes whatever value the user checks in the checkbox). I researched it and I just don't know how to do that and everything I did so far is wrong. Thanks a lot!!

      <form action="" ng-click="change(key)">
        <input ng-model="key.call"type="checkbox"">Call
        <br>
        <input ng-model="key.person"type="checkbox" >Person
        <br>
        <input type="checkbox"ng-model="key.dial">Dial
        <br>
        <input type="checkbox" ng-model="key.voice">Voice          
    </form>

app.controller('appCtrl', function($scope) {
    $scope.companies = [{
        name: 'The Best Company Denim',
        users: [{
            firstName: 'Alex',
            lastName: 'D',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Sarah',
            lastName: 't',
            number: 14,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J',
            lastName: 'd',
            number: 07,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company Elegant',
        users: [{
            firstName: 'Alx',
            lastName: 'B',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Seth',
            lastName: 'w',
            number: 12,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'J.S',
            lastName: 'B',
            number: 7.
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }, {
        name: 'The Best Company by Julia',
        users: [{
            firstName: 'Aleddddx',
            lastName: 'l',
            number: 1234,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Maggy',
            lastName: 'n',
            number: 1,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }, {
            firstName: 'Ja',
            lastName: 'Key',
            number: 123,
            call: '',
            person: '',
            dial: '',
            voice: ''
        }]
    }]

    $scope.change = function(key) {
        for (var i = 0; i < $scope.companies[0].users; i++) {
            $scope.companies[0].users[i].call: key)
    }
}
});

According to the code above, $scope.companies[0].users is an array, which have a length property that gives you the number of items in it. Then your code will be:

$scope.change = function(key) { for (var i = 0; i < $scope.companies[0].users.length; i++) { $scope.companies[0].users[i].call = key; }

If you are looking for looping through companies too, then nest 2 for loops, one to loop companies and another one to loop users with the same idea given in above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM