The .join(format)
method is great for displaying array elements
with your own customization. Whatever string you put inside as an argument becomes the connectors for the elements
of the array.
JavaScript has a default way of displaying its variables. When it comes to displaying an array, the default setting
is simply array.join(",");
What's the opposite of join? It's .split(splitMarker)
. For
this, put in a string and the computer sends back an array of items. It finds what you put into the splitMarker
,
removes it everywhere it sees it, and sends the pieces back as array elements. It's .join()
in reverse!
This one cuts out the letter "x" and sends back all the pieces. Oops, we accidentally split up the "oxen" into "o" and "en"! I guess that means we need to be careful when using this.
By the way, if you pass in an empty string "", you get back an array of all the single characters!
This is a little off-topic, but it relates. You may have seen a file here and there that ends with
.csv
. CSV means comma separated value. If you use a spreadsheet program like
Microsoft Excel, Google Sheets, or Apple's Numbers, you may have seen an option to save a file as a .csv
file.
Why am I bringing this up? Well, CSV files are pretty common out there and instead of being intimidated by them, I just wanted to mention what they really are at their core. Can you guess?
Well, imagine that each cell in your spreadsheet is an element in an array. If you did
mySpreadsheetArray.join(",");
, you'd
essentially have what a CSV file looks like inside.
And what basically happens when another spreadsheet program (or other viewer) tries to read a CSV file? You guessed
it! It essentially runs myCSVFile.split(",");
on it to get all the cell values back.
There's a little more to it behind the scenes, but CSV files are commonly used because spreadsheet programs tend to add a lot of other data to a file, like the fonts and colors you used, and so on. By trimming down to CSV and then importing it, you clean up the file and keep only the data, which allows other programs to understand it.
|
1 |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 |