如何使用javascript循环这些值?

时间:2011-12-09 07:36:46

标签: javascript extjs extjs4

如何使用javascript循环低于值?

0, -120, -240, -360, -480, -600, -720

获取输出

0 -120 -240 -360 -480 -600 -720 0 -120 -240 -360 -480 -600 -720 0 -120 -240 -360 -480 -600 -720 .....

此致

4 个答案:

答案 0 :(得分:1)

使用嵌套的for循环:

var cycles = 10;
var numbers = [0, -120, -240, -360, -480, -600, -720];
for (var i = 0; i < cycles; i++) {
    for (var j = 0; j < numbers.length; j++) {
        // concatenate here
    }
}

请参阅此fiddle

答案 1 :(得分:1)

这个怎么样,

var cycles = 10;
var current_cycle = 0
var numbers = [0, -120, -240, -360, -480, -600, -720];
for (var i = 0; i < numbers.length; i++) {
    document.write( numbers[i] )
    if( (i+1) == numbers.length ){ // last item in the current cycle
        current_cycle++;
        if(current_cycle < cycles){
            i = 0; // re-starts the loop
        }
    }
}

答案 2 :(得分:1)

或者:

var d = -120, n = 7, s = 0, c = n * 2, limit = d * n;
var result = []
while (c--) {
  result.push(s % limit);  
  s += d;
}
alert(result); // 0,-120,-240,-360,-480,-600,-720,0,-120,-240,-360,-480,-600,-720

答案 3 :(得分:0)

var arr = [0, -120, -240, -360, -480, -600, -720];
var count = 3;
var output = [];
for(var i = 0; i < count; i++)
{
   output = output.concat(arr);
}
alert(output);

working code on fiddle