无法从JSON数组中检索值

时间:2022-09-13 13:20:48

I get the data from server every 10 seconds as

我每10秒从服务器获取数据

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();

and then I want to extract certain values which I do as

然后我想提取某些值

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
}

However on Browser console.log, I see

然而在浏览器控制台。日志,我看到

[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]}

where each Object when I click, I get

当我点击每个对象时,我得到了什么

0: Object
dateTime: "2015-08-31 19:56:28.335000"
moduleId: "110010 "
reading: "400"
__proto__: Object

What is wrong with my code?

我的代码有什么问题?

1 个解决方案

#1


3  

You never return from your .map calls

你永远不会从你的。map调用中返回

readings['reading'] = apiResponse.map(function(value){ return value.reading })
readings['dateTime'] = apiResponse.map(function(value){ return value.dateTime })

Also, you don't have to stringify your object to see it in the console with preceding text, simply do:

同样,您不必对对象进行字符串化,以便在控制台中看到前面的文本,只需:

console.log('Temperature reading: ', readings)

#1


3  

You never return from your .map calls

你永远不会从你的。map调用中返回

readings['reading'] = apiResponse.map(function(value){ return value.reading })
readings['dateTime'] = apiResponse.map(function(value){ return value.dateTime })

Also, you don't have to stringify your object to see it in the console with preceding text, simply do:

同样,您不必对对象进行字符串化,以便在控制台中看到前面的文本,只需:

console.log('Temperature reading: ', readings)