join - Concatenates an Array together
Purpose: The Join method concatenates the elements of an Array, and returns a string.
By default the Join method will separate each item in the original array with a comma (","). For example,
var myArray = ["one", "two", "three"];
var joinedArray = myArray.join();
document.write(joinedArray);
joinedArray will now be a string "one,two,three"
It is possible to change the separator used, so instead of having comma-delimitated you could have space delimitated:
joinedArray = myArray.join(" ");
one two three
Or you could have nothing in between each element in the Array:
joinedArray = myArray.join("");
onetwothree
You can even use multiple characters, like a word to separate the elements.
joinedArray = myArray.join(" and ");
one and two and three
I have been using this a lot with breaking up sentences into their individual characters, and then putting the entire sentence back together again
var mySentence = "Hello wonderful world";
var myArray = mySentence.split(""); //This splits the sentence into individual elements in the array
--Now I can loop through the array and do whatever I want to each letter--
mySentence = myArray.join(""); //Putting it back into mySentence
No comments:
Post a Comment