spacer.png, 0 kB
spacer.png, 0 kB
Home arrow Javascript arrow Javascript: Array concat
Javascript: Array concat PDF Print E-mail
Written by Administrator   
domenica, 31 agosto 2008

The Javascript concat method is very useful to manage Arrays in Javascript. You can add different arrays as well as append elements "on the fly".

In the following example we add two values to our Array and we print (with an alert) the first added:

<script language="Javascript">

Array1 = ["value1", "value2", "value3"];
Value4 = window.prompt("new element... ");

Array1 = Array1.concat(Value4);
alert(Array1[3]);

</script>

In the following example instead we use the concat method to join two different Arrays. Array2 will contain Array1 and then Array2 values.

<script language="Javascript">

Array1 = ["value1", "value2", "value3"];
Array2 = ["value21", "value22", "value23"];

Array2 = Array2.concat(Array1);

</script>

Finally in the following example the concat method is useful to join three Arrays.
All the elements will be stored in another Array called FinalArray.

<script language="Javascript">

Array1 = ["value1", "value2", "value3"];
Array2 = ["value21", "value22", "value23"];
Array3 = ["value31", "value32", "value33"];

FinalArray = Array1.concat(Array2, Array3);

</script>

Last Updated ( domenica, 31 agosto 2008 )
 
< Prev   Next >
spacer.png, 0 kB
spacer.png, 0 kB
spacer.png, 0 kB