Ad

Javascript For Loop Using Variable

- 1 answer

Can I use variable to do the second for loop so it list out all data in GroupA1 and GroupB1.

Tried but fail.. anyone have idea how to make it work?

for example

function Loop(){
    var AllGroup=["GroupA1","GroupB1"]
    var GroupA1=["A1","A2"]
    var GroupB1=["B1","B2"]
    var Group=""

for(var i = 0; i < AllGroup.length; i++){
    Group=AllGroup[i]

    var SubGroup=""
    for(var x = 0; x < Group.length; x++){
        SubGroup=Group[x]
        alert(SubGroup);
    }
}
}
Ad

Answer

Hopefully my sample code can help you to deal with your question

For example

<script>
    var GroupA1=["A1","A2"]
    var GroupB1=["B1","B2"]
    var Group=""
    var AllGroup = [GroupA1, GroupB1]    
for(var i = 0; i < AllGroup.length; i++){
    Group=AllGroup[i]

    var SubGroup=""
    for(var x = 0; x < Group.length; x++){
        SubGroup=Group[x]
        alert(SubGroup);
    }
}
</script> 
Ad
source: stackoverflow.com
Ad