英文:
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 = ["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 = x.count();
console.log("Count: ", counter);
}, 1000);
<!-- end snippet -->
for example: console.log("Count: ", 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 = ["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);
<!-- 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 = ["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);
答案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: "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 you're 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论