The result of this method is a new array consisting of the original array, plus the concatenation.
The values that are passed to the method are added to the end of the array.
If arrays are passed, they are flattened and their individual elements added.
The method returns an array consisting of the original Array plus the concatenated values.
If Array1 contains "AAA", "BBB", "CCC" and Array2 contains "000", "111", "222", then the method call Array1.concat(Array2) will return an array with all the elements in a single collection. The original arrays will be untouched.
The concat() method will flatten arrays that are passed as arguments. However, it will not recursively flatten multi-dimensional arrays.
// Create two arrays and demonstrate concat() method myArray1 = new Array("AAA", "BBB", "CCC"); myArray2 = new Array("000", "111", "222"); document.write("Array1<BR>") displayArrayAsTable(myArray1); document.write("Array2<BR>") displayArrayAsTable(myArray2); document.write("Result returned from Array1.concat(Array2)<BR>") displayArrayAsTable(myArray1.concat(myArray2)); document.write("Result returned from Array1.concat('AAA')<BR>") displayArrayAsTable(myArray1.concat("AAA")); document.write("Result returned from Array1.concat('AAA').concat('DFG')<BR>") displayArrayAsTable(myArray1.concat("AAA").concat("DFG")); // Display an array in a table function displayArrayAsTable(anArray) { myLength = anArray.document.write("<TABLE BORDER=1>"); for(myIndex = 0; myIndex < myLength; myIndex++) { document.write("<TR><TD>"); document.write(myIndex); document.write("</TD><TD>"); document.write(anArray[myIndex]); document.write("</TD></TR>"); } document.write("</TABLE><BR><BR>") }
See also: | Array.prototype, String.concat() |
Prev | Home | Next |
Array.Class | Up | Array.constructor |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |