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

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

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

问题

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

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

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

Uncaught TypeError: x.count is not a function

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

以下是代码:

  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"];
  2. console.log(countArray.length);
  3. var j = 0;
  4. function countval() {
  5. return countArray[j++];
  6. }
  7. setInterval(function() {
  8. var counter = j - 1;
  9. console.log("Count: ", counter);
  10. }, 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 -->

  1. 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;];
  2. console.log(countArray.length);
  3. var j = 0;
  4. function countval() {
  5. return countArray[j++];
  6. }
  7. setInterval(function() {
  8. var counter = 0;
  9. x = countval();
  10. console.log(&quot;X value: &quot;, x);
  11. counter = x.count();
  12. console.log(&quot;Count: &quot;, counter);
  13. }, 1000);

<!-- end snippet -->

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

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 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

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

  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"];
  2. console.log(countArray.length);
  3. var j = 0;
  4. function countval() {
  5. return countArray[j++];
  6. }
  7. setInterval(function() {
  8. if(j <= countArray.length){
  9. var counter = 0;
  10. console.log("Count: ", j);
  11. console.log("X value: ", countval());
  12. }
  13. }, 1000);
英文:

Your problem is solved with this code

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

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

  1. 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;];
  2. console.log(countArray.length);
  3. var j = 0;
  4. function countval() {
  5. return countArray[j++];
  6. }
  7. setInterval(function() {
  8. if(j &lt;= countArray.length){
  9. var counter = 0;
  10. console.log(&quot;Count: &quot;, j);
  11. console.log(&quot;X value: &quot;, countval());
  12. }
  13. }, 1000);

<!-- end snippet -->

答案2

得分: 1

您的脚本将如下所示:

  1. var countArray = [
  2. "2020-14-03 11:14:48.225000",
  3. "2020-14-03 11:14:48.226000",
  4. "2020-14-03 11:14:48.227000",
  5. "2020-14-03 11:14:48.228000",
  6. "2020-14-03 11:14:48.229000",
  7. "2020-14-03 11:14:48.230000",
  8. "2020-14-03 11:14:48.231000",
  9. "2020-14-03 11:14:48.232000",
  10. "2020-14-03 11:14:48.233000",
  11. "2020-14-03 11:14:48.234000",
  12. "2020-14-03 11:14:48.235000",
  13. "2020-14-03 11:14:48.236000",
  14. "2020-14-03 11:14:48.237000",
  15. "2020-14-03 11:14:48.238000",
  16. "2020-14-03 11:14:48.239000",
  17. "2020-14-03 11:14:48.240000",
  18. "2020-14-03 11:14:48.241000",
  19. "2020-14-03 11:14:48.242000",
  20. "2020-14-03 11:14:48.243000",
  21. "2020-14-03 11:14:48.244000"
  22. ];
  23. console.log(countArray.length);
  24. var j = 0;
  25. function countval() {
  26. return countArray[j++];
  27. }
  28. setInterval(function() {
  29. var counter = 0;
  30. x = countval();
  31. console.log("X value: ", x);
  32. counter = countArray.indexOf(x);
  33. console.log("Count: ", counter);
  34. }, 1000);

希望这有所帮助。

英文:

Your script will be like this:

  1. 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;];
  2. console.log(countArray.length) ;
  3. var j = 0;
  4. function countval()
  5. {
  6. return countArray[j++];
  7. }
  8. setInterval(function(){
  9. var counter = 0;
  10. x= countval();
  11. console.log(&quot;X value: &quot;, x);
  12. counter=countArray.indexOf(x);
  13. console.log(&quot;Count: &quot;, counter);
  14. }, 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:

  1. setInterval(function(){
  2. if(j < countArray.length){
  3. x = countval();
  4. console.log("X value: ", x);
  5. console.log("Count: ", j);
  6. }
  7. }, 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:

  1. setInterval(function(){
  2. if(j &lt; countArray.length){
  3. x = countval();
  4. console.log(&quot;X value: &quot;, x);
  5. console.log(&quot;Count: &quot;, j);
  6. }
  7. }, 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:

确定