0

I'm implementing a feature which will allow me to dynamically add columns into a JavaScript table:

for(var i = 0; i < info.length; i++){

        var temp = [];

        temp.push(parseInt(info[i].site_id));
        temp.push(info[i].site);
        temp.push(info[i].site_code);
        temp.push(processArray(info[i].tc10));
        temp.push(processArray(info[i].tc9x_test));
        temp.push(processArray(info[i].tc9x_build));
        temp.push(processArray(info[i].oracle_dev));
        temp.push(processArray(info[i].database));
        temp.push(processArray(info[i].baseline));
        temp.push(processArray(info[i].push_windows));
        temp.push(processArray(info[i].push_unix));
        temp.push(processArray(info[i].license));
        temp.push(processArray(info[i].tcx));
        temp.push(processArray(info[i].eng));
        temp.push(processArray(info[i].perforce_proxy));
        temp.push(processArray(info[i].volume_server));
        temp.push(info[i].windows_ref_unit_location);
        temp.push(info[i].unix_ref_unit_location);
        temp.push(info[i].windows_rte_location);
        temp.push(info[i].unix_rte_location);
        temp.push(info[i].windows_toolbox_location);
        temp.push(info[i].unix_toolbox_location);
        temp.push(info[i].UGII_LICENSE_FILE);
        temp.push(info[i].UGS_LICENSE_SERVER);
        temp.push(info[i].unix_dev_units);
        temp.push(info[i].unix_devop_path);
        temp.push(info[i].perforce_proxy_path);
        temp.push(info[i].primary_contact);
        temp.push(info[i].secondary_contact);
        temp.push(info[i].num_users);
        temp.push(info[i].TC_12);

            // check if new columns got added:
        if(len > 29){
            for(var j = 30; j < len; j++){
                var col = columns[j];
                temp.push(info[i].col);
            }
        }
            rows.push(temp);
    }
    return rows;
}

var rows = [[]] holds the table data ... info[[]] contains the JSON objects queried from the database. The problem in on this piece of code:

for(var j = 30; j < len; j++){
    var col = columns[j];
    temp.push(info[i].col);
}

I'm trying to dynamically bind col with some of the attributes of info. But I don't know whether is possible or not ... How could I do that? Suppose a user added a new column, TC_12. Thus, I don't know TC_12 exists, so I want to dynamically bid col into info[i] so it could somehow yield me info[i].TC_12. Any ideas?

1
  • 1
    You can clean that big series of pushes up by just putting them in the literal: gist.github.com/3185075 Commented Jul 26, 2012 at 22:53

1 Answer 1

1

Use square bracket notation to use the value of a variable or the result of some other expression as the object property.

temp.push(info[i][col]);

FYI, you can do all those pushes with a single call to .push() by passing multiple arguments...

    temp.push(parseInt(info[i].site_id),
              info[i].site,
              info[i].site_code,
              processArray(info[i].tc10),
              processArray(info[i].tc9x_test),
              // etc...
             );
Sign up to request clarification or add additional context in comments.

2 Comments

It sounds great, but it's not giving a 2-D matrix ... so Google Visualization API is complaining about that ...
@philippe: Not sure why that would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.