Javascript loop odd / even in array

I am trying to make a loop in an array that sorts the contents and creates a div with two values โ€‹โ€‹each.

Tried a lot of things, but I canโ€™t understand what I need to do.

This is what I need to do: loop in an array and create a div. Each div must have 2 values โ€‹โ€‹for the array. Like this:

<div id="1">content, content 2</div> <div id="2">content 3, content 4</div> <div id="3">content 5, content 6</div> <div id="4">content 7</div> 

So I have an array like this:

 var myArray = ['content', 'content 2', 'content 3', 'content 4', 'content 5', 'content 6', 'content 7']; 

And I repeat like this:

 for(var i=0; i<=myArray.length; i++){ if(currentDiv == 1){ $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); console.log(myArray[i]); } if(currentDiv == 2){ $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); console.log(myArray[i]); $('#container').append("</div>"); } console.log(currendDivID); if(currentDiv == 3){ currendDivID ++; $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } if(currentDiv == 4){ console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); $('#container').append("</div>"); } console.log(currendDivID); if(currentDiv == 5){ currendDivID++; $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } if(currentDiv == 6){ if(myArray[i] == undefined){ return; } console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); $('#container').append("</div>"); } if(currentDiv == 7){ currendDivID++; if(myArray[i] == undefined){ $('#container').append("</div>"); return; } $('#container').append("<div id=\"s" + currendDivID + "\">"); console.log(myArray[i]); $('#s'+currendDivID+'').append("<p>"+myArray[i]+"</p>"); } currentDiv ++; } 

This works fine, but I need to hardcode each div to count and then fill. Is there a better way to do this and not encode the number of elements like I did on currentDiv var?

thanks

+4
source share
3 answers

You can use the module operator % to find out even and odd, as this will give you the remainder after division. If you divide the number by 2, and the remainder is zero, then it is even, and if the remainder is 1, then it is odd.

 for(var i=0; i<=myArray.length; i++){ if(currentDiv % 2 == 0){ } else{ } } 

You can do currentDiv using a loop increment variable, also I think the loop counter performs one additional iteration that I set up by running loop from i=1 .

 for(var i=1; i<=myArray.length; i++){ if(i % 2 == 0){ $('#s' + i + '').append("<p>"+myArray[i]+"</p>"); console.log(myArray[i]); } else{ console.log(myArray[i]); $('#s'+ i + '').append("<p>"+myArray[i]+"</p>"); $('#container').append("</div>"); } } 
+3
source

use the module operator.

so currentDiv % 2 == 0 for even currentDiv % 2 == 1 for odd

what he does is divide by 2 and compare the remainder (http://en.wikipedia.org/wiki/Euclidean_division)

0
source
 var arr=['content','content2','content3','content4','content5']; for(var i=0;i<=arrlen;i++){ if(i%2==0){ if(i!=(arrlen-1)){ if(i!=arrlen){ console.log(arr[i]+" "+arr[i+1]); }else{ break; } }else{ console.log(arr[i]); } } } 
0
source

Source: https://habr.com/ru/post/1440903/


All Articles