简体   繁体   中英

time array only one loop

I'm trying to get an array containing hour:minutes using only one loop. so at the end I can get something close to: [0:00, 0:01... 23:58, 23:59] I know I can do it using two for loops such as:

for (var i=0;i<24;i++) {
    for (var j=0;j<60;j++) {
        // push i and j accordingly
    }
}

Here's my go:

var times = [];
for (var i=0;i<60*24;i++) {
    times.push(((i/60)|0) + ':' + i%60 );
}
console.log(times)

(i/60)|0 is a stupid way to do Math.floor now that I'm looking at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM