views:

64

answers:

4

I have the following dynamically generated strings:

var stringA = ["a1", "a2", "a3" ... 'a400' ... 'a600']; // length 600
var stringB = ["b1", "b2", "b3" ... 'b400']; // length 400

How can I get an Array or string of both combined like this:

var myString = ["a1b1", "a2b2", "a3b3" ... "a400b400", "a401" ... "a600"]
A: 

I don't think there's anything built into the Array object that will do it for you, you'll have to do the loop. The loop is trivial, though:

var index, length;
var result = [];
// assertion: arrayA.length === arrayB.length
result.length = arrayA.length; // Helps performance in some implemenations, harmless in others
for (index = 0, length = arrayA.length; index < length; ++index) {
     result[index] = arrayA[index] + arrayB[index];
}

(I've renamed stringA -> arrayA and stringB -> arrayB to avoid confusion.)

If the arrays are different lengths or some of the entries in the arrays are undefined (which is totally possible, JavaScript arrays are sparse), you'll want to handle that in the loop, e.g.:

var index, length, Apresent, Bpresent;
var result = [];
result.length = Math.max(arrayA.length, arrayB.length); // Helps performance in some implementations, harmless in others
for (index = 0, length = result.length; index < length; ++index) {
     Apresent = arrayA.hasOwnProperty(index);
     Bpresent = arrayB.hasOwnProperty(index);
     if (Apresent && Bpresent) {
         result[index] = arrayA[index] + arrayB[index];
     }
     else if (Apresent) {
         result[index] = arrayA[index];
     }
     else if (Bpresent) {
         result[index] = arrayB[index];
     }
}

If the arrays are sparse and they both happen to be sparse at the same index, the resulting array will also be sparse.

T.J. Crowder
This will also result in a lot of `undefined` when the array lengths do differ, as they do in the question :)
Nick Craver
@Nick: Thanks. It wasn't at all clear from the question originally that the arrays might be different lengths (he showed them as the same length), but I see what you mean in his example result (subtle, that). I saw your other comment and was updating as you commented here. :-) I see someone's fixed the question for him.
T.J. Crowder
A: 
var myString = [];

for (var i=0;i<stringA.length;i++){
     myString[i] = stringA[i] + stringB[i];
}
NimChimpsky
This won't work, you'll get `"undefined"` in many places when the array lengths differ, as in the question.
Nick Craver
+3  A: 

You can do something like this:

var result = [], len = Math.max(stringA.length, stringB.length);
for(var i=0; i < len; i++) {
    result.push((stringA[i] || "") + (stringB[i] || ""));
}

You can test it out here, the || "" is to prevent getting undefined as a string on the for the array that's shorter. The Math.max() call is to allow either A or B to be longer, it'll iterate to the end of either, just as A is longer in the question.

Nick Craver
I deleted my answer because I forgot that I *should* use `Math.max()` to avoid iterating past a smaller array. So +1 to you Nick!
alex
Thanx Nick! You've helped me out again!
Mircea
This will result in entries in the destination array at indexes where there aren't entries in either source array (if there are any such entries).
T.J. Crowder
@TJCrowder - If you look at the question, that's what the OP as asking for originally, a went to 600, b went to 400 :)
Nick Craver
@Nick: Just sayin', if we're going to handle empty slots, let's handle empty slots. :-)
T.J. Crowder
A: 

If you do not need to retain the the items in the original 2 arrays then the code below works really well. It does not have a predetermined limit to the number of iterations in the for loop which means that the stringA and StringB array could continue to grow in size while this code is running.

var myString = [];
for(var answer; answer=(stringA.shift()||"") + (stringB.shift()||"");) {
    myString.push(answer);
}
Eric