How do I access all the column names of any table and store them as a list?

Students are trying to make a library with a function that checks for columns with images, independent of what table has been imported.

In order to perform the simple string check, they need to first reference those cells by using traversals. For that, we need a list with all column names, which can then be traversed. How do I do that?
The getColumn() function only works if you know the name of the column.

Link to the project or level: Code.org
What I expect to happen: Find a way to get all the column names of a table into a list.
What actually happens: There is no such way according to my knowledge.
What I’ve tried: Some searches suggested using the getTable() function, but it doesn’t exist in the app lab. In fact, I can find no way to store the table as an object into a variable, or access columns without knowing their names.

Hi there @parth.patel,

While I don’t fully understand the whole behaviors of the library’s that code.org uses any code given in the library… not the datasets the easiest solution is to just add all the extra data in the dataset to the main project, either that… or storing all those values locally in a variable and then inject them into the dataset upon loading the library the first time

I’m not sure if this is the answer your looking for… but i hope this helps!

Hi. The link takes me to am empty project.

I can’t see the project, but I would look for an item in the first row that either has a png or jpg extension. I will assume that the item is a picture. I will also assume that all of the items in the same column are images.

Thank you for your replies. It is a blank project as we are in the planning stage and I don’t even know if something like this is possible in App Lab.

Let me explain more… I want to make a library function which takes the index as a parameter and traverses that row through all the columns of a table (not a specific table, it should work with any table… hence the column names are unknown) and returns the column names with the string ā€œ/imagesā€ in them (ie, the columns with image links). OR it will return the image links themselves… that’s something the students can decide later.

Also, we know how to traverse vertically (ie loop through a range of indices) but how to we traverse horizontally (through a range of columns)?

Another question that is related - I know of just one way in App Lab to access data: using getColumn(tablename, columnname). Is there any other way?

How about automating this inside a loop that would get the names of columns and store them into numbered variables (thinking of something like:

for (var i=0; i<n, i++){
var ā€œcolā€+i = getcolumn(tablename, column i);
}
where n is the number of columns in a table)?

OR is there a way to save the table object inside a variable? like:
var myTable = getTable(ā€œUS States and Territoriesā€);

Thanks a lot!

@parth.patel,
FAQ:

#1: I want to make a library function which takes the index as a parameter and traverses that row through all the columns of a table (not a specific table, it should work with any table… hence the column names are unknown) and returns the column names with the string ā€œ/imagesā€ in them

// ?? instructions unclear, this is doable but requires intracate knowledge of loops
function getColumns(table, callback) {
  readRecords(table, [], function (v) {
    var columns = {};
    for (var c in v[0]) {
      columns[c] = [];
    }
    for (var i = 0; i < v.length; i++) {
      for (var n in v[i]) {
        columns[n].push(v[i][n]);
      }
    }
    callback(columns);
  })
}
// use case in where all columns are formatted in a general array
getColumns("Your Table", function (col) {
  console.log(col);
})

#2: Also, we know how to traverse vertically (ie loop through a range of indices) but how to we traverse horizontally (through a range of columns)?

This has been answered previous by the demo function given up top, to move vertically you’ll need the whole dataset by using, readRecords, this will provide all necessary data from the given table with a small hit to performance… which i doubt you care about

#3: Another question that is related - I know of just one way in App Lab to access data: using getColumn(tablename, columnname). Is there any other way?

Well yes… this was the question proposed based on question #2 was it not? you can find this in the documentation that code.org gives in their documentation since it is one of their listed blocks, you should have no trouble finding it

#4: How about automating this inside a loop that would get the names of columns and store them into numbered variables (thinking of something like:

// DEMO PESUDO CODE PROPOSED BY #4
for (var i=0; i<n, i++){
var ā€œcolā€+i = getcolumn(tablename, column i);
}

where n is the number of columns in a table)?

OR is there a way to save the table object inside a variable? like:

var myTable = getTable(ā€œUS States and Territoriesā€);

So… I’m going to go out on a limb here and say all your problems will most likely be solved by readRecords since this was asked the same time on #2,3,4 along with 1 asking for a scalable function

But to answer your question no, you wanted dynamic column support to be read by any table, what if my naming scheme doesn’t include an iterable number… your program breaks and it won’t support what i want to use which i assume was most likely the conundrum you were having and probably couldn’t explain the exact behaviour you were looking for, most times it’s just best to try and dumb it down a bit which is something I’ve been trying to improve upon myself recently

Also My apologies for any typos, syntax errors, or grammar mistakes since this was written in notepad and I don’t exactly feel like fixing anything at the moment, anywho hope this helps!

1 Like

Dear Varrience: I feel so little after reading your explanation. My respects!

Hi @parth.patel,

We’d love to help! Can you please confirm that this is not for a student’s Create PT response? I see that another user on the forum responded, but the moderation team is unable to assist with debugging at this time of year unless we receive this confirmation.

Thank you!
–Michael K.

@varrience Thank you for your detailed explanation. Yes, I believe the readRecords method should help me out. I’m swamped with finishing up the gradebooks this week but I’ll give it a shot later and share my results. However, most of the column names don’t end with an iterable number. As you correctly pointed out, this poses a problem.

@rossiai Please don’t! You tried to help, and that is always appreciated!

@mkmietowicz Yes, I can confirm that this is for their ā€˜Make a Library’ assignment for CSP Unit 7 and not for a Create PT assignment.