Creating 2D array in javascript

Hi All,

I have a below code in which I am trying to fill the data from table that has few rows and columns. The problem is that I am unable to get the right value at indexes outside the loop. I need to add values at each row of the array and return the array to use in other functions. Something like arr = [{abc,asd,fgh},{aa,dfg,uio},{asd,fgh,dfg}]

function TableValues() {
    var TableValues = [[],[]];
    var Table;

    Table = tablerefrence;
    for (let i = 0; i < Table.GetRowCount(); i++) {
        for (let j = 0; j < Table.GetColumnCount(); j++) {
            TableValues[[i][j]] = Table.getValueAt(i, j);
        }
    }  
   Cosole.Log(TableValues[[1][0]]); //incorrect value :frowning: 
}

This isn’t how array indexing works in JavaScript? What language is this?

This is javascript. I always used var[i] = something; to assign values to single dim array. They worked fine.
Here, I tried to define 2d array empty using [][] not sure if that’s correct.

TableValues[[i][j]] = Table.getValueAt(i, j);
In the above line, I am trying to add values from the table row i and col j to the TableValues array on indexes [{00,01,02},{10,11,12}....] and so on.

For example, if arr is an 2D array variable . How can I add values to its indexes 00, 01 and so on in javascript? So in the end I might be able to display arr[0], arr[1] … with the values being added? If we look at the structure of 2D arrays, it’s somehting like [[values at 00, values at 01],[value at 10, values at 11],[value at 20, value at 21]]

Have a look at the following challenges to better understand arrays.

And just to be clear, JS doesn’t really have multi dimensional arrays, it has arrays of arrays. It is a semantic difference, but may affect your thinking.

1 Like

Yeah, sort of, I mean a multi dimensional array is a subset. To me a two dimensional array is more restricted and usually notated differently. But yeah, it can function as one. Like I said, semantics.

I originally learned in C. And that was also what I was taught in JS - that distinction. It makes sense to the way my brain works. But again, it’s semantics. I just thought I’d throw that out there - I’ve had a few cases where that “clicked” with people. If not, just ignore it.

Thanks everyone. I got it working with the help of the topics @camperextraordinaire shared in his reply.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.