simbada simbada - 5 months ago 8
AngularJS Question

finding property with true flag in an object

I have an object with three properties.

$scope.badges = {'Full Service': false, 'Negotiable': true, 'Under Warranty': false};


How do i
extract OR find property
from it with
true
flag?

Basically, i want to find property from this object having true flag and then assign it to another scope variable.

My HTML

<div ng-repeat="(badge,enabled) in badges" class="checkbox">
<label>
<input type="checkbox" ng-model="badges[badge]" /> {{badge}}
</label>
</div>


Angular Controller

$scope.badges = {
'Full Service History': false,
'Negotiable': false,
'Under Warranty': false,
' Urgent': false
};


I want to see which checkbox user has selected.

Answer

A much cleaner solution:

$scope.activeKeys = [];
for (var key in $scope.badges) {
    if ($scope.badges[key]) {
        $scope.activeKeys.push(key);
    }
}