admin管理员组

文章数量:1390707

I'm trying to do a loop in a array that sorts the content and create div's with 2 values each.

Tried a lot of things, but I can't figure it out what I need to do.

This is what I need to do: loop in a array and create div's. Each div should 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 the array like this:

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

And I'm looping 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 hard code each div to count and then populate. Is there any better way to do it and not harcoded the number of elements like I did on currentDiv var?

Thanks

I'm trying to do a loop in a array that sorts the content and create div's with 2 values each.

Tried a lot of things, but I can't figure it out what I need to do.

This is what I need to do: loop in a array and create div's. Each div should 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 the array like this:

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

And I'm looping 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 hard code each div to count and then populate. Is there any better way to do it and not harcoded the number of elements like I did on currentDiv var?

Thanks

Share Improve this question asked Oct 20, 2012 at 0:46 Anderson OliveiraAnderson Oliveira 2511 gold badge3 silver badges9 bronze badges 1
  • in your example, the values in the array are sequential with how they should be entered into the div - is that really the case? – kmfk Commented Oct 20, 2012 at 0:53
Add a ment  | 

3 Answers 3

Reset to default 3

You can use % modulus operator to find out the even and odd as it give you remainder after division. If dividing number by 2 and remainder is zero then it even and if remainder is 1 then its odd.

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

   } 
   else{

   } 
}

You can currentDiv with i the loop increment variable, also I think loop counter is doing one extra iteration which I have adjust by starting 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>");
   } 
}

use the modulus operator.

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

what it does is divide by 2 and pares the remainder (http://en.wikipedia/wiki/Euclidean_division)

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]);
            }

        }

    }

本文标签: phpjavascript loop oddeven in arrayStack Overflow