JavaScript
the following code, except for the different conditions of judgment, is the same in the loop. The judgment condition may contain I
, or it may not contain I
. Is there any way to simplify them?
// obj json
loop1(){
for(let i=0;i<obj.length;iPP){
if(i%2!==0){
doSomethingA()
} else {
doSomethingB()
}
}
}
loop2(){
for(let i=0;i<obj.length;iPP){
if(i%2!==0 && i!==0){
doSomethingA()
} else {
doSomethingB()
}
}
}
loop3()
loop4()