Sixdas Sixdas - 1 year ago 148
Javascript Question

Repeat array in javascript

I have to repeat an array in loop mode. In this way:

A, B; C; D; E, A, B; C; D; E, A, B; C; D; E, A, B; C; D; E, A, B; C; D; E, A, B; C; D; E, A, B; C; D; E

var a = "A;B;C;D;E;";

var b = a.split(';');

for(var i = 0; i < b.length; i++)
$('#ul').append('<li>'+ b[i] +'</li>');

<script src=""></script>
<ul id="ul"></ul>

But I can not repeat it. I would like to understand the method to use because my real sopo and to create a calendar with the shifts. In practice, once you set a start date the array has to be repeated on the calendar. Example:

array = (A, B, C, D, E):

  • 26.08.2016 A

  • 27/08/2016 B

  • 28.8.2016 C

  • 29.08.2016 D

  • 08.30.2016 E

  • 08.31.2016 A

  • 01/09/2016 B

  • 09/02/2016 C

  • 09.03.2016 D

  • 09.04.2016 E

Example Image

I hope you get the idea and you have some advice for me.

thank you

example php script

$data = '03/10/2014';
$turni = array('sera','pomeriggio','Mattina','Notte','Riposo');

$n_g_mesi = array(1=>31,28,31,30,31,30,31,31,30,31,30,31);
list($gg,$mm,$aa) = explode('/', $data);
$gg = (int)$gg;
$mm = (int)$mm;
$n_g_q_mese = $n_g_mesi[$mm];

$numero_turni = 10;

$n = count($turni);
for($i=0; $i<$numero_turni; $i++){
echo $data.' '.$turni[$i%$n]."<br />\n";
$gg = $gg%$n_g_q_mese+1;
$mm = $mm%12+1;
if($mm==3 && $aa%4==0 && $n_g_q_mese==28){ // se l'anno รจ bisestile
$mm = 2;
$gg = $n_g_q_mese = 29;
$n_g_q_mese = $n_g_mesi[$mm];
$data = str_pad($gg,2,'0',STR_PAD_LEFT).'/'.str_pad($mm,2,'0',STR_PAD_LEFT).'/'.$aa;

Answer Source

If you need to repeat things multiple times, than loop multiple times

Nested loop

var b = ["A", "B", "C", "D", "E"];
for(var i=0;i<3;i++) {
    for(var j=0;j<b.length;j++) {

or Loop the number of times and use a Modulus based on length or the array.

var b = ["A", "B", "C", "D", "E"];
for(var i=0;i<20;i++) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download