Java Reference
In-Depth Information
Concatenating Elements
The concat(arg1, arg2,...) method creates and returns a new array that contains the
elements of the array object on which the method is invoked followed by the specified
arguments in order. If an argument is an array, the array's elements are concatenated.
If an argument is a nested array, it is flattened one level. The original array and arrays
passed as arguments are not modified by this method. If the original array or arguments
contain objects, a shallow copy of objects is made. Examples of using the concat()
method are as follows:
var names = ["Fu", "Li"];
// Assigns ["Fu", "Li", "Do", "Su"] to names2
var names2 = names.concat("Do", "Su");
// Assigns ["Fu", "Li", "Do", "Su", ["Lu", "Zu"], "Yu"] to names3
var names3 = names.concat("Do", ["Su", ["Lu","Zu"]], "Yu");
Joining Array Elements
The join(separator) method converts all elements of the array to Strings, concatenates
them using separator , and returns the resulting string. If separator is unspecified, a
comma is used as the separator. The method works on all elements of the array starting
from the index 0 to the index equal to length - 1 . If an element is undefined or null , the
empty string is used. Here are some examples of using the join() method:
var names = ["Fu", "Li", "Su"];
var namesList1 = names.join(); // Assigns "Fu,Li,Su" to namesList1
var namesList2 = names.join("-"); // Assigns "Fu-Li-Su" to namesList2
var ids = [10, 20, , 30, undefined, 40, null]; // A sparse array
var idsList1 = ids.join(); // Assigns "10,20,,30,,40," to idsList1
var idsList2 = ids.join("-"); // Assigns "10-20--30--40-" to idsList2
Reversing Array Elements
The reverse() method rearranges the elements of the array in reverse order and returns
the array. Examples of using this method are as follows:
var names = ["Fu", "Li", "Su"];
names.reverse(); // Now, the names array contains ["Su","Li","Fu"]
// Assigns "Fu,Li,Su" to reversedList
var reversedList = names.reverse().join();
 
Search WWH ::




Custom Search