将变量添加到现有的json对象

时间:2015-01-30 17:40:00

标签: angularjs angularjs-scope

request = myService.getCases();
request.then(
function(payload) {
$scope.cases = payload.data;

var time =  Math.floor((Date.now() - Date.parse($scope.cases[i].date_case_modified))/(60000*60*24));

$scope.cases.duration.push(time);
}
});

在控制器内部我试图将cases.duration添加到Cases对象上,但它不会将它添加到返回的对象上。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我认为您只需要引入forEach,如下所示:

request = myService.getCases();
request.then(
    function(payload) {
         $scope.cases = payload.data;

         angular.forEach($scope.cases, function (el) {
            var time =  Math.floor((Date.now() - Date.parse(el.date_case_modified))/(60000*60*24));

            el.duration = time;
        });
    }
});

希望这有帮助