I ran into a slightly quirky behavior with the Table object today, and figured I’d leave a note in case anyone else runs into this.
What I’m doing: I’m converting a large spreadsheet to a JSON, for use in a website. The spreadsheet is the contents of a small periodical library, where each row has a Location, and a set of Topics in that location.
This JSON is designed for cross-referencing. Each object represents one Location, with a list of Topics — and each of these Topics includes a list of other Locations where that specific topic appears. The end result looks like this:
var allData = [
{ "Location" : "01-01",
"Entries" : [
{ "Topic" : "Aardvark",
"TopicLocations" : ["01-01", "02-02", "03-03"]},
{ "Topic" : "Airplane",
"TopicLocations" : ["01-01", "03-48", "12-78"]}
]
},
{ "Location" : "01-02",
"Entries" : [
{ "Topic" : "Bananas",
"TopicLocations" : ["01-02", "14-56", "92-32"]},
{ "Topic" : "Bangladesh",
"TopicLocations" : ["01-02", "33-58", "04-57"]}
]
}]
So, my initial thought was just to load the .tsv into a Table object, and (1) do the standard (TableRow outerRow : table.rows()) {
loop, and (2) inside of this loop, do another (TableRow innerRow : table.rows()) {
loop to find the matching topic string on other rows.
This doesn’t work. Or rather, it doesn’t work if you call table.rows() on the same table object. If you want to do a nested TableRow loop like this, you’ll need to create a second Table object (which loads the same .tsv or .csv file).
So you’ll end up with (in variable declaration and loading):
Table outerTable, innerTable;
outerTable = loadTable("my-data.tsv", "header, tsv");
innerTable = loadTable("my-data.tsv", "header, tsv");
And the nested loop will look like this:
for (TableRow outerRow : outTable.rows()) {
// whatever
for (TableRow innerRow : innerTable.rows()) {
// more whatever
}
}
That’s all I got! Hope this saves some head-scratching time in case anyone else runs into this. Cheers