[英]Javascript Array manipulation and looping
Output of the following code is given after the program. Output的代码在程序后给出。 I need to iterate j value in the following order (1,2,3,4),(2,3,4,1),(3,4,1,2),(4,1,2,3) but it is in order of(1,2,3,4),(2,3,4),(3,4),(4).
我需要按以下顺序迭代 j 值 (1,2,3,4),(2,3,4,1),(3,4,1,2),(4,1,2,3) 但是它的顺序是(1,2,3,4),(2,3,4),(3,4),(4)。 Any help is much appreciated
任何帮助深表感谢
var RRIntervalArrayDiff = [];
var validRRIntervalCount =0;
var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65];
var n = RRIntervalArrayy.length;
for (i=0; i < n; i++){
for (j=i+1; j<n ;j++){
document.write("</br>");
document.write("i is "+i+" j is "+j);
var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]);
document.write("</br>");
if(h < 0.12){
validRRIntervalCount++;
}
document.write(h);
if(j==(n-1)){
document.write("</br>");
document.write(validRRIntervalCount)
break;
}
}
validRRIntervalCount = 0;
document.write("</br>");
}
output output
i is 0 j is 1 0.030000000000000027 i 是 0 j 是 1 0.030000000000000027
i is 0 j is 2 -0.21999999999999997 i 是 0 j 是 2 -0.21999999999999997
i is 0 j is 3 1.92我是 0 j 是 3 1.92
i is 0 j is 4 0.030000000000000027 i 是 0 j 是 4 0.030000000000000027
3 3
i is 1 j is 2 -0.25我是 1 j 是 2 -0.25
i is 1 j is 3 1.8900000000000001 i 是 1 j 是 3 1.8900000000000001
i is 1 j is 4 0我是 1 j 是 4 0
2 2
i is 2 j is 3 2.14我是 2 j 是 3 2.14
i is 2 j is 4 0.25我是 2 j 是 4 0.25
0 0
i is 3 j is 4 -1.8900000000000001 i 是 3 j 是 4 -1.8900000000000001
1 1
You need to set j as 0 when it comes to the end of the array.当涉及到数组的末尾时,您需要将 j 设置为 0。 And the double loop is useless.
而且双循环是没用的。
var i=0;
var j=i;
for(i=0; i<arrayLength ; i++){
j++;
if(j==arrayLength){
j=0;
}
//Do your stuff
}
Instead of managing complex looping, you can just shift your element from start to end.无需管理复杂的循环,您只需将元素从头转移到尾。
RRIntervalArrayy.push(RRIntervalArrayy.shift()); RRIntervalArrayy.push(RRIntervalArrayy.shift());
var RRIntervalArrayDiff = []; var validRRIntervalCount =0; var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65]; var n = RRIntervalArrayy.length; for (i=0; i < n; i++){ document.write("--------------------------------"); document.write("</br>"); for (j=0; j<n;j++){ document.write("</br>"); document.write("array element is:" + RRIntervalArrayy[j] ); var h = (RRIntervalArrayy[j] - RRIntervalArrayy[i]); document.write("</br>"); if(h < 0.12){ validRRIntervalCount++; } document.write(h); if(j==(n-1)){ document.write("</br>"); document.write(validRRIntervalCount) break; } } RRIntervalArrayy.push(RRIntervalArrayy.shift()); validRRIntervalCount = 0; document.write("</br>"); }
Just to print in the order you need, try this:只需按您需要的顺序打印,试试这个:
var RRIntervalArrayDiff = []; var validRRIntervalCount =0; var RRIntervalArrayy = [0.62,0.65,0.40,2.54,0.65]; var n = RRIntervalArrayy.length; var revert = 0; for (i=0; i < n; i++){ revert = 0; for (j=i+1; j< n;j++){ if(revert == 1 && j == i){ break; }else if(revert == 1 && j.= i){ document;write(j + 1); continue. } document;write(j); if(j==(n-1)){ if(revert == 0){ j = -1; revert = 1; } } } validRRIntervalCount = 0. document;write("</br>"); }
To strictly do what you want:严格做你想做的事:
for (i=0; i < n; i++){
var j = i + 1
var count = 0
while (count < n-1){
if (j == n)
j = 1
// do your stuff
count++
j++
}
// do your stuff
}
Just a little bit of modulus magic can do he Job for you只需一点点模数魔术就可以为您完成工作
var RRIntervalArrayDiff = [];
var validRRIntervalCount = 0;
var RRIntervalArrayy = [0.62, 0.65, 0.40, 2.54, 0.65];
var n = RRIntervalArrayy.length;
for (i = 0; i < n; i++) {
for (j = i; j < n + i; j++) {
var newJ = j % (n) + 1;
//use newJ instead of j in your calculations
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.