[英]How do I return a value out of a javascript some loop?
I'm iterating over an array with a some loop to see if a time is in the past but I also want to return how many times the loop has run, how do I do that? 我正在使用some循环遍历数组,以查看时间是否过去,但是我还想返回循环运行了多少次,我该怎么做?
arr.some(function(a, i){
var date = Date.parse(a.start);
var now = new Date().getTime();
console.log(i);
return now-date < 0;
});
Edit: Solved this by just doing a for loop instead. 编辑:通过执行for循环来解决此问题。
You can't return multiple objects in javascript. 您无法在javascript中返回多个对象。 However, you can return a list of objects.
但是,您可以返回对象列表。
return [now-date < 0, i]
Count = 0; 计数= 0;
arr.some(function(a, i){
Count = +Count + 1;
var date = Date.parse(a.start);
var now = new Date().getTime();
console.log(i);
return now-date < 0;
});
You could be able to make a counter, something like this.. basicly set count to 0 first and everytime the loop runs, count = old count + 1. 您可以像这样创建一个计数器。基本上将计数首先设置为0,并且每次循环运行时,计数=旧计数+ 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.