在`setInterval`内部计算数组中的元素数量:Javascript

huangapple go评论68阅读模式
英文:

Count no. of elements in an array inside setInterval : Javascript

问题

我有一个包含元素的数组。

我想要的是,在JavaScript中,每次Set Interval函数加载时都计算元素的数量。

类似于计数器,但当我使用count函数时,它显示一个错误:

Uncaught TypeError: x.count is not a function

我只想要在setinterval函数发生时每次从0开始计算值的数量。

以下是代码:

var countArray = ["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000","2020-14-03 11:14:48.233000","2020-14-03 11:14:48.234000","2020-14-03 11:14:48.235000","2020-14-03 11:14:48.236000","2020-14-03 11:14:48.237000","2020-14-03 11:14:48.238000","2020-14-03 11:14:48.239000","2020-14-03 11:14:48.240000","2020-14-03 11:14:48.241000","2020-14-03 11:14:48.242000","2020-14-03 11:14:48.243000","2020-14-03 11:14:48.244000"];

console.log(countArray.length);

var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
  var counter = j - 1;
  console.log("Count: ", counter);
}, 1000);

例如:console.log("Count: ", counter); 应该在每秒后打印:

0
1
2
3
4
5
6
7
8
9

更新:
我的问题是字符串格式的。我想要计算数组中字符串元素的出现次数。

英文:

I have an array containing elements .

What I want is to count the no. of elements every time when the Set Interval function loads in javascript.

Something like a counter but when I am using the count function it's displaying an error :

> Uncaught TypeError: x.count is not a function

I just want to count the no. of values when setinterval function occurs every time starting from 0 .

Here is the Code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var countArray = [&quot;2020-14-03 11:14:48.225000&quot;,&quot;2020-14-03 11:14:48.226000&quot;,&quot;2020-14-03 11:14:48.227000&quot;,&quot;2020-14-03 11:14:48.228000&quot;,&quot;2020-14-03 11:14:48.229000&quot;,&quot;2020-14-03 11:14:48.230000&quot;,&quot;2020-14-03 11:14:48.231000&quot;,&quot;2020-14-03 11:14:48.232000&quot;,&quot;2020-14-03 11:14:48.233000&quot;,&quot;2020-14-03 11:14:48.234000&quot;,&quot;2020-14-03 11:14:48.235000&quot;,&quot;2020-14-03 11:14:48.236000&quot;,&quot;2020-14-03 11:14:48.237000&quot;,&quot;2020-14-03 11:14:48.238000&quot;,&quot;2020-14-03 11:14:48.239000&quot;,&quot;2020-14-03 11:14:48.240000&quot;,&quot;2020-14-03 11:14:48.241000&quot;,&quot;2020-14-03 11:14:48.242000&quot;,&quot;2020-14-03 11:14:48.243000&quot;,&quot;2020-14-03 11:14:48.244000&quot;];

console.log(countArray.length);


var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
  var counter = 0;
  x = countval();
  console.log(&quot;X value: &quot;, x);
  counter = x.count();
  console.log(&quot;Count: &quot;, counter);
}, 1000);

<!-- end snippet -->

for example: console.log(&quot;Count: &quot;, counter); should print:

0
1
2
3
4
5
6
7
8
9

after each second.

Update:
My queston is of string format. how so I want to count the no. of string elements occurence in an array.

答案1

得分: 1

你的问题已经通过这段代码解决了:

var countArray = ["2020-14-03 11:14:48.225000","2020-14-03 11:14:48.226000","2020-14-03 11:14:48.227000","2020-14-03 11:14:48.228000","2020-14-03 11:14:48.229000","2020-14-03 11:14:48.230000","2020-14-03 11:14:48.231000","2020-14-03 11:14:48.232000","2020-14-03 11:14:48.233000","2020-14-03 11:14:48.234000","2020-14-03 11:14:48.235000","2020-14-03 11:14:48.236000","2020-14-03 11:14:48.237000","2020-14-03 11:14:48.238000","2020-14-03 11:14:48.239000","2020-14-03 11:14:48.240000","2020-14-03 11:14:48.241000","2020-14-03 11:14:48.242000","2020-14-03 11:14:48.243000","2020-14-03 11:14:48.244000"];

console.log(countArray.length);

var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
  if(j <= countArray.length){
    var counter = 0;
    console.log("Count: ", j);
    console.log("X value: ", countval());
  }
}, 1000);
英文:

Your problem is solved with this code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var countArray = [&quot;2020-14-03 11:14:48.225000&quot;,&quot;2020-14-03 11:14:48.226000&quot;,&quot;2020-14-03 11:14:48.227000&quot;,&quot;2020-14-03 11:14:48.228000&quot;,&quot;2020-14-03 11:14:48.229000&quot;,&quot;2020-14-03 11:14:48.230000&quot;,&quot;2020-14-03 11:14:48.231000&quot;,&quot;2020-14-03 11:14:48.232000&quot;,&quot;2020-14-03 11:14:48.233000&quot;,&quot;2020-14-03 11:14:48.234000&quot;,&quot;2020-14-03 11:14:48.235000&quot;,&quot;2020-14-03 11:14:48.236000&quot;,&quot;2020-14-03 11:14:48.237000&quot;,&quot;2020-14-03 11:14:48.238000&quot;,&quot;2020-14-03 11:14:48.239000&quot;,&quot;2020-14-03 11:14:48.240000&quot;,&quot;2020-14-03 11:14:48.241000&quot;,&quot;2020-14-03 11:14:48.242000&quot;,&quot;2020-14-03 11:14:48.243000&quot;,&quot;2020-14-03 11:14:48.244000&quot;];

console.log(countArray.length);


var j = 0;

function countval() {
  return countArray[j++];
}

setInterval(function() {
if(j &lt;= countArray.length){
  var counter = 0;
  console.log(&quot;Count: &quot;, j);
  console.log(&quot;X value: &quot;, countval());
}
}, 1000);

<!-- end snippet -->

答案2

得分: 1

您的脚本将如下所示:

var countArray = [
  "2020-14-03 11:14:48.225000",
  "2020-14-03 11:14:48.226000",
  "2020-14-03 11:14:48.227000",
  "2020-14-03 11:14:48.228000",
  "2020-14-03 11:14:48.229000",
  "2020-14-03 11:14:48.230000",
  "2020-14-03 11:14:48.231000",
  "2020-14-03 11:14:48.232000",
  "2020-14-03 11:14:48.233000",
  "2020-14-03 11:14:48.234000",
  "2020-14-03 11:14:48.235000",
  "2020-14-03 11:14:48.236000",
  "2020-14-03 11:14:48.237000",
  "2020-14-03 11:14:48.238000",
  "2020-14-03 11:14:48.239000",
  "2020-14-03 11:14:48.240000",
  "2020-14-03 11:14:48.241000",
  "2020-14-03 11:14:48.242000",
  "2020-14-03 11:14:48.243000",
  "2020-14-03 11:14:48.244000"
];

console.log(countArray.length);

var j = 0;
function countval() {
  return countArray[j++];
}

setInterval(function() {
  var counter = 0;
  x = countval();
  console.log("X value: ", x);
  counter = countArray.indexOf(x);
  console.log("Count: ", counter);
}, 1000);

希望这有所帮助。

英文:

Your script will be like this:

  var countArray = [&quot;2020-14-03 11:14:48.225000&quot;,&quot;2020-14-03 11:14:48.226000&quot;,&quot;2020-14-03 11:14:48.227000&quot;,&quot;2020-14-03 11:14:48.228000&quot;,&quot;2020-14-03 11:14:48.229000&quot;,&quot;2020-14-03 11:14:48.230000&quot;,&quot;2020-14-03 11:14:48.231000&quot;,&quot;2020-14-03 11:14:48.232000&quot;,&quot;2020-14-03 11:14:48.233000&quot;,&quot;2020-14-03 11:14:48.234000&quot;,&quot;2020-14-03 11:14:48.235000&quot;,&quot;2020-14-03 11:14:48.236000&quot;,&quot;2020-14-03 11:14:48.237000&quot;,&quot;2020-14-03 11:14:48.238000&quot;,&quot;2020-14-03 11:14:48.239000&quot;,&quot;2020-14-03 11:14:48.240000&quot;,&quot;2020-14-03 11:14:48.241000&quot;,&quot;2020-14-03 11:14:48.242000&quot;,&quot;2020-14-03 11:14:48.243000&quot;,&quot;2020-14-03 11:14:48.244000&quot;];

  console.log(countArray.length) ;


var j = 0;
  function countval() 
  {
   return countArray[j++];
  }

setInterval(function(){
  var counter = 0;
  x= countval();
  console.log(&quot;X value: &quot;, x);
  counter=countArray.indexOf(x);
console.log(&quot;Count: &quot;, counter);
  }, 1000); 

答案3

得分: 1

The error is quite simple. You're assigning x = countVal(); and countVal() returns a string value, so basically you're doing this: "2020-14-03 11:14:48.225000".count(); which is an incorrect statement. So, Firstly remove this line from your code. Other than that I'm having difficulty understanding what you're trying to do, from reading your question I assumed you want something like this:

setInterval(function(){
    if(j < countArray.length){
        x = countval(); 
        console.log("X value: ", x);
        console.log("Count: ", j);
    }
}, 1000);

And if you want to show the length of each element of the array just edit your .count() to .length

英文:

The error is quite simple. You're assigning x = countVal(); and countVal() returns a string value, so basically you're doing this: &quot;2020-14-03 11:14:48.225000&quot;.count(); which is an incorrect statement. So, Firstly remove this line from your code. Other than that I'm having difficulty understanding what you're trying to do, from reading you're question I assumed you want something like this:

setInterval(function(){
    if(j &lt; countArray.length){
        x = countval(); 
        console.log(&quot;X value: &quot;, x);
        console.log(&quot;Count: &quot;, j);
    }
}, 1000);

And if you want to show the length of each element of the array just edit your .count() to .length

huangapple
  • 本文由 发表于 2020年1月6日 14:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定