我正在从这样的资源中获取数据:
var result = someService.getData();
result.$promise.then(function() {
console.log(result);
});
这给了我以下结果:
1: "200"
2: "1810"
$promise: Object
$resolved: true
__proto__: f
我需要将这些数据放入一个 javascript 数组中,如下所示:
[[1, 200], [2, 1810]]
我试图在 forEach
中这样做:
var data = [];
angular.forEach(monthly, function(value, key) {
var tmp = [];
tmp[0] = key;
tmp[1] = value;
data.push(tmp);
});
但是 $promise
和 $resolved
属性也出现在 foreach 中。这看起来也不是最好的方法。
我应该如何以正确的方式执行此操作?
最佳答案
简短的回答是调用 .toJSON()
- 这样做会去除 $promise 和 $resolved 属性并允许您正确地使用 angular.forEach
就像在旧版本的 Angular 中一样。
类似于:
var result = someService.getData();
result.$promise.then(function(value) {
console.log(value.toJSON());//will strip the annoying properties, use here
});
关于javascript - 将 $promise 数据放入 Javascript 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595191/