我每个10 seconds
从服务器获取数据
var data = { response: {}, calls: 0 };
var poller = function() {
$http.get('http://localhost:8080/monitoring/rest/graph/temperature').then(function(r){
data.response = r.data;
console.log(r.data);
extractTemperatureReadings(r.data)
data.calls++;
$timeout(poller, 10000)
});
}
poller();
然后我想提取某些值
function extractTemperatureReadings(apiResponse) {
var readings = {}
readings['reading'] = apiResponse.map(function(value){value.reading})
readings['dateTime'] = apiResponse.map(function(value){value.dateTime})
console.log('Temperature reading: ' + JSON.stringify(readings))
return readings
}
但是在浏览器
console.log
上,我看到了[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
monitoring.js:64 Temperature reading: {"reading":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"dateTime":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]}
当我单击每个对象时,
0: Object
dateTime: "2015-08-31 19:56:28.335000"
moduleId: "110010 "
reading: "400"
__proto__: Object
我的代码有什么问题?
最佳答案
您从不会在return
通话中出现.map
readings['reading'] = apiResponse.map(function(value){ return value.reading })
readings['dateTime'] = apiResponse.map(function(value){ return value.dateTime })
另外,您无需对对象进行字符串化即可在控制台中以前面的文本看到它,只需执行以下操作:
console.log('Temperature reading: ', readings)
关于javascript - 无法从JSON数组检索值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32340342/