...

View Full Version : Merge 2 Arrays



Kal
07-04-2011, 03:11 PM
Hi,

I am trying to merge the values of 2 arrays.

array 1 = 1, 2, 3, 4, 5

array 2 = a, b, c, d, e

Resulting in:

array 3 = 1-a, 2-b, 3-c, 4-d, 5-e etc

both arrays that will be used originally will always of the same length. Both arrays will contain numbers

Can you please help?

Thanks

Kal
07-04-2011, 03:28 PM
Can't seem to find much in Jquery that will do this for me.

Philip M
07-04-2011, 03:53 PM
You say "both arrays will contain numbers" but a,b,c,d,e are strings. I am assuming that the numbers in your first array are in fact string values.

<script type = "text/javascript">

var arr1 = ["1", "2", "3", "4", "5"];
var arr2 = ["a","b","c","d","e"];
var arr3 = [];
var len = arr1.length;
for (var i = 0; i<len; i++) {
arr3[i] = arr1[i] + "-" + arr2[i];
}
document.write (arr3);

</script>


Quizmaster: The phrase "Workers of the world unite" is inscribed on the tomb of which political thinker in Highgate Cemetery, north London?
Contestant: Enoch Powell.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum