0

I am trying to loop through an array of strings using several for loops to call each array item. I'm having some trouble getting just the right code to pull it off. I have tried using global definitions of arrays and so on.

var An = new Array(            
       "11111100011111",       
       "11111000011111",      
       "11110011001111", 
       "11100000000111", 
       "11001111110011", 
       "10011111111001");  
var Bn = new Array(            
       "1000000001",       
       "1001111001",      
       "1000000001", 
       "1000000001", 
       "1001111001", 
       "1000000001"); 

// This second part sits in a onload function.
var alphabet = "AB".split("");

for (x = 0; x < 6; x++) {       
    for (i = 0; i < 2; i++) {
     var my_object = {};
     my_object = window[alphabet[x] + 'n[' + i + ']'];
     my_object = window[my_object].replace(/0/g, "B");
     my_object = window[my_object].replace(/1/g, "_");
    }
}
7
  • 1
    Why are you making this so complicated? Commented Sep 25, 2014 at 16:15
  • I have a lot of arrays to run through. Commented Sep 25, 2014 at 16:28
  • 2
    What are you trying to achieve? Where do you set name? Why does x go until 6, if you use alphabet[x], and alphabet.length==2? Commented Sep 25, 2014 at 16:41
  • 1
    I'm still not understanding. What significance does alphabet have here? What is window[my_object] supposed to accomplish? Where are you trying to access An and Bn? Commented Sep 25, 2014 at 16:54
  • 3
    window[alphabet[x] + 'n[' + i + ']']; is wrong and should be window[alphabet[x]+'n'][i]; if at all. However, you shouldn't use window like that, rather define your maps as var arrays = {A:[…],B:[…]} and access them normally there Commented Sep 25, 2014 at 17:01

1 Answer 1

2

What you should do is put An and Bn into an object. This will make it much easier to reference later on.

var alphabet = {
    An: [
       "11111100011111",       
       "11111000011111",      
       "11110011001111", 
       "11100000000111", 
       "11001111110011", 
       "10011111111001"
    ],
    Bn: [
       "1000000001",       
       "1001111001",      
       "1000000001", 
       "1000000001", 
       "1001111001", 
       "1000000001"
    ]
};

Then you pick out the array you want and loop over it.

var arrays = ['A', 'B'];
for(var x = 0, xLen = arrays.length; x < xLen; x++){
    var my_key = arrays[x]+'n';
    for(var i = 0, iLen = alphabet[my_key].length; i < iLen; i++){
        alphabet[my_key][i] = alphabet[my_key][i].replace(/0/g, "B");
        alphabet[my_key][i] = alphabet[my_key][i].replace(/1/g, "_");
    }
}

Actually, for the inner for loop, you can also use Array.map.

var arrays = ['A', 'B'];
for(var x = 0, xLen = arrays.length; x < xLen; x++){
    var my_key = arrays[x]+'n';
    alphabet[my_key] = alphabet[my_key].map(function(val){
        val = val.replace(/0/g, "B");
        val = val.replace(/1/g, "_");
        return val;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.