PDA

View Full Version : javascript concat() method to join arrays



tc101
September 21st, 2009, 08:03 PM
I am confused about using the concat() method to join two arrays. In the example below, why does the first write show all 6 values in the array, but the second only shows 3? How do I permanently join 2 arrays into 1?

<html>
<body>
<script type="text/javascript">

var arr = new Array(3);
arr[0] = "Jani";
arr[1] = "Tove";
arr[2] = "Hege";

var arr2 = new Array(3);
arr2[0] = "John";
arr2[1] = "Andy";
arr2[2] = "Wendy";

document.write(arr.concat(arr2)); // why does this show all 6 values

document.write("<br>" + arr); // but this only shows 3

</script>
</body>
</html>

tc101
September 21st, 2009, 09:10 PM
I figured this out.

The concat() method is used to join two or more arrays.

This method does not change the existing arrays, it only returns a
copy of the joined arrays.

Tom combine arr1 and arr2 into arr3 this works:


var arr3 = arr1.concat(arr2);
document.write("<br> arr3 = " + arr3);

The Cog
September 21st, 2009, 09:11 PM
http://www.w3schools.com/jsref/jsref_concat_array.asp

concat() creates a new third array without modifying the original two.

tc101
September 21st, 2009, 09:11 PM
Is there some way I can close or delete this thread since I have answered it?