Javascript 从复选框循环遍历json对象

Javascript 从复选框循环遍历json对象,javascript,json,angularjs,Javascript,Json,Angularjs,我有一个json对象,它是通过ng repeat中的复选框加载的。它列出了可能的服务,我试图在提交表单时访问已检查的服务 我正在通过以下方式加载复选框: <label style="margin-right:10px" ng-repeat="item in dsServces"> <input type="checkbox" name="selectedFruits" value="{{item.DefaultServic

我有一个json对象,它是通过ng repeat中的复选框加载的。它列出了可能的服务,我试图在提交表单时访问已检查的服务

我正在通过以下方式加载复选框:

<label style="margin-right:10px" ng-repeat="item in dsServces">
    <input
        type="checkbox"
        name="selectedFruits"
        value="{{item.DefaultServiceID}}"
        ng-model="expense.dsService[item.DefaultServiceName]"
    > {{item.DefaultServiceName}}
</label>
返回(当选择一对时)

但我不知道如何循环,所以它只显示服务名称,以便我可以根据客户添加它们

在后端,我有:

$scope.expense = {
    tags: []
};
在提交时:

 angular.forEach($scope.expense, function(item) {
    console.log(item);
});
它吐出
对象{Email:true,Backups:true}
,但我无法计算出一个循环,它只返回哪个对象是真的

我的主要目标是拥有类似

angular.forEach(forloop) {
    //service.item should be like 'Email' whatever the item.DefaultServiceName is
    // the loop should be all items that are checked
    addService(service.item);
});

如果我理解正确,您希望将对象的键值推送到数组中。如果是这样的话,只需对您的
forEach()进行一点更改即可。

像这样:

// The first parameter is what you want to loop through
// Second is the iterator, you can separate out the key and values here
// The third is the context, since I put the array we want to push to in here
// we just need to call `this.push()` within the iterator
angular.forEach($scope.expense, function(value, key) {
  this.push(key);
}, $scope.expense.tags);

如果有帮助,请告诉我。

不清楚。制作小提琴,以便有人能帮助你。
angular.forEach(forloop) {
    //service.item should be like 'Email' whatever the item.DefaultServiceName is
    // the loop should be all items that are checked
    addService(service.item);
});
// The first parameter is what you want to loop through
// Second is the iterator, you can separate out the key and values here
// The third is the context, since I put the array we want to push to in here
// we just need to call `this.push()` within the iterator
angular.forEach($scope.expense, function(value, key) {
  this.push(key);
}, $scope.expense.tags);