无法从JSON数组中检索值

时间:2015-09-01 20:29:22

标签: javascript json angularjs

我每隔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

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:3)

您的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)