简体   繁体   中英

how to repeat the loop of object properties twice in for / in function

How can I repeat the following 'times' object in the for/in loop twice?

var times = []
, periods = ['am', 'pm']
, hours = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
, prop = null
, hour = null;

for (prop in periods) {
for (hour in hours) {
  times.push(('' + hours[hour]) + ':' + '00' + periods[prop]);
}

} // object properties from 12:00am to 11:00pm

so I want the output is [12:00am .... 11:00pm (first time loop) ... 12:00am ... 11:00 pm (second time loop]. Any idea? Thanks

Build two arrays and concatenate them:

var times1 = []
, times2 = []
, times = []
, periods = ['am', 'pm']
, hours = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
, prop = null
, hour = null;

for (prop in periods) {
for (hour in hours) {
  times1.push(('' + hours[hour]) + ':' + '00' + periods[prop]);
  times2.push(('' + hours[hour]) + ':' + '00' + periods[prop]);
}

} // object properties from 12:00am to 11:00pm

times = times1.concat(times2);

Try using the *2 function if that doesn't work make a variable that is double of the repeat times.

Add this loop around the other 2 loops and see if that works.

for (2) {

}

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