簡體   English   中英

循環嵌套迭代的Javascript

[英]Javascript for loop nested iteration

我正在嘗試遍歷一大堆值,並每秒收集一個值的平均值。 我無法使此代碼正常工作,據我所知,問題出在嵌套的while循環中。 我是否在發生范圍錯誤,使我無法迭代for循環索引?

數據是一個以毫秒為單位的時間戳和一個輻射計數。 a.data[i][0]是時間戳, a.data[i][26]是計數。

for (i = 0; i < a.data.length; i++){
    // counts is the count of radiation over the last timeframe
    var counts = 0;
    // t1 is the start time 
    // t2 is the current iteration time
    var t1, t2 = a.data[i][0];
    while ((t2 - t1) < 1000){
        t2 = a.data[i][0];
        counts += a.data[i][26];
        i++;
    }
    // Geiger Data is an array of { x:(time), y:(value)} datapoints.
    GeigerData.push({x: (t1/1000), y: counts});
}

您的問題源於此行:

 var t1, t2 = a.data[i][0];

定義JS變量並不能那樣工作,並且在代碼中t1始終是未定義的。 你真正想要的是

 var t1 = a.data[i][0];
 var t2 = t1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM