gpt4 book ai didi

javascript - 无法在循环外的控制台日志中显示数组值?

转载 作者:行者123 更新时间:2023-12-03 01:16:08 25 4
gpt4 key购买 nike

我使用(cpu_usage,时间戳和clientID)将数组推到循环之外。现在我想读取clientID的控制台日志值 console.log(result[0][2]); 。但是它向我显示错误“无法读取未定义的属性'2'”。有人知道哪里出了问题吗?另外,如果我在循环内调用这个数组,它就可以工作

数组内容:content of array我使用 console.log(result[0][2]); error 后出错

<div id="cont"></div>
<script type="text/javascript">

$.getJSON( "http://localhost:8000/api/devices", function( res) {


var result= [];
var devNames = new Array();
console.log(result[0][2]);
$.each( res, function(i) {
var deviceNames=data[i].clientAllias;
var clientId=data[i].clientId;
devNames .push(deviceNames);
$.each( res[i].clientData, function(a) {
$.each( res[i].clientData[a], function(key, val) {
clientId2=res[i].clientData[a].clientId
var cpu=res[i].clientData[a].cpuUsage;
var time_usages=res[i].clientData[a].timestamp;
final=[];
final.push(time_usages, cpu, clientId2);
result.push(final);

});
});
});

result.sort();



$(document).ready(function(){
var Object = {
marker: {
states: {
enabled: true,
}
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: 'Sec'
}, {
count: 1,
type: 'hour',
text: 'Min'
},
{
count: 1,
type: 'day',
text: 'Hours'
},

{
type: 'all',
text: 'All'

}],
title:'hours',
inputEnabled: true,
_selected: 1
},


title: {
text: clientNames,
},
xAxis: {

title: {
enabled: true,
text: 'CPU USAGE'
},
type: 'datetime',
dateTimeLabelFormats: {
second: '%H:%M:%S',
minute: '%H:%M',
hour: '%H:%M',


},


},
plotOptions: {
series: {
marker: {
enabled: false,

}
}
},

series: [{
name:"CPU USAGE",
data: result,
}],

chart: {
renderTo: 'cont'
}

};

var chart = new Highcharts.StockChart(dataObject);
//var chart = $('#container').highcharts('StockChart', dataObject);



});
});




</script>

最佳答案

您在错误的位置进行控制台日志记录。当您记录时,结果中没有任何内容,因此当您尝试访问嵌套元素时会出错。

将控制台日志放在最后(result.sort()之后),您应该得到您需要的内容

关于javascript - 无法在循环外的控制台日志中显示数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52006804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com