如何检索JSON中的键/值对象的JavaScript? [英] How to retrieve the key/value object in Json with Javascript?
本文介绍了如何检索JSON中的键/值对象的JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个奇怪的问题。
首先,我检索在同一时间几个电话。并保存在一个名为价值观的变量返回的数据。
First I retrieve several calls at the same time. And save the returned data in a variable called "values"
function PrefsService($resource,PrefsResource,$q) {
var initialize = function() {
return $q
.all(
[PrefsResource.get({key:"TwentyFourHourTime"}),
PrefsResource.get({key:"DecimalTime"}),
PrefsResource.get({key:"startDayOfWeek"}),
PrefsResource.get({key:"RoundingIncrement"}),
PrefsResource.get({key:"RoundingOption"})
]
)
.then(function(values) {
return values
})
我用这块code在控制器看到返回值:
I use this piece of code in controller to see the returned value:
PrefsService
.initialize()
.then(function(values) {
console.log("values",values);
console.log("values[0]",values[0]);
console.log("values[0].result",values[0].result);
})
我想用值[0]。结果得到的结果对象。但它总是给我的价值不确定。
I want to use "values[0].result" get the result object. But it always gives me a value of "undefined".
为什么?
THX
推荐答案
最简单的方法是返回的实际值,而不是承诺:
The most straightforward way would be to return the actual values, not the promises:
function PrefsService($resource,PrefsResource,$q) {
var initialize = function() {
return $q
.all([
PrefsResource.get({key:"TwentyFourHourTime"}),
PrefsResource.get({key:"DecimalTime"}),
PrefsResource.get({key:"startDayOfWeek"}),
PrefsResource.get({key:"RoundingIncrement"}),
PrefsResource.get({key:"RoundingOption"})
])
.then(function(values) {
var returnValues = [];
values.forEach(function(v) {
v.then(function(a) {
returnValues.push(a);
})
});
return returnValues;
})
};
return {
initialize:initalize;
}
}
PrefsService
.initialize()
.then(function(values) {
console.log(values); //an array of the actual values, not the promises
})
这篇关于如何检索JSON中的键/值对象的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文