根据不同于零的变化计算事件数

时间:2017-02-09 01:28:29

标签: javascript dom

我下面的代码目前读入的是一个数据集,我将其添加到该函数中。它将该列中的值的总和计算为this.sum。现在我想扩展这段代码的功能。

我的数据集有很多0,后面跟着很短的值,或者“ events ”。我想根据我的数据从0到一个值的次数找到这些“事件”的数量。此外,我希望将少于五个零的事件分开,以便处于相同的事件中。

所以,在我的数据集下面,我希望它说“发生了2件事。”

我仍想保留'this.sum',但现在希望存储this.events

我习惯使用像Python和Matlab这样的脚本语言,并且是Javascript的新手,所以感谢任何帮助!

processData: function(data) {

        // convert our data from the file into an array
        var lines = data.replace(/\n+$/, "").split("\n");

        // we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
        lines = lines.map(Number).filter(Boolean);

        // now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
        this.sum = lines.reduce((a, b) => a + b, 0);
        this.sum = this.sum.toFixed(2);
    },

数据:

0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352

修改 以下为ev提供了不正确的值,无论我对count所做的更改(从5更改为10,更改为15)(我的数据集包含的数字与上面给出的数据集不同。)但是当我将值更改为500时,它会下降,所以我更高到12,000,它给了我this.events的值,这是我期待的。

processData: function(data) {

        // convert our data from the file into an array
        var lines = data.replace(/\n+$/, "").split("\n");

var ev = 0; // the event counter (initialized to 0)
for(var i = 0, count = 0; i < data.length; i++) { // 'count' will be the counter of consecutive zeros
  if(lines[i] != 0) { // if we encounter a non-zero line
    if(count > 5) ev++; // if the count of the previous zeros is greater than 5, then increment ev (>5 mean that 5 zeros or less will be ignored)
    count = 1; // reset the count to 1 instead of 0 because the next while loop will skip one zero
    while(++i < lines.length && lines[i] != 0) // skip the non-zero values (unfortunetly this will skip the first (next) zero as well that's why we reset count to 1 to include this skipped 0)
      ;
  }
  else // if not (if this is a zero), then increment the count of consecutive zeros
    count++;
}
this.events = ev;

        // we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
        lines = lines.map(Number).filter(Boolean);

        // now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
        this.sum = lines.reduce((a, b) => a + b, 0);
        this.sum = this.sum.toFixed(2);
    },

1 个答案:

答案 0 :(得分:1)

var ev = 0; // the event counter (initialized to 0)
for(var i = 0, count = 0; i < lines.length; i++) { // 'count' will be the counter of consecutive zeros
  if(lines[i] != 0) { // if we encounter a non-zero line
    if(count > 5) ev++; // if the count of the previous zeros is greater than 5, then increment ev (>5 mean that 5 zeros or less will be ignored)
    count = 1; // reset the count to 1 instead of 0 because the next while loop will skip one zero
    while(++i < lines.length && lines[i] != 0) // skip the non-zero values (unfortunetly this will skip the first (next) zero as well that's why we reset count to 1 to include this skipped 0)
      ;
  }
  else // if not (if this is a zero), then increment the count of consecutive zeros
    count++;
}
this.events = ev;