简体   繁体   English

用JavaScript创建二维矩阵

[英]Creating a 2-D matrix in Javascript

I'm using Google Visualization API to create a table. 我正在使用Google Visualization API创建表。 In order to setup the head of the column, this is valid matrix: 为了设置列的开头,这是有效的矩阵:

var rows = [['Id', 'Site', 'Site Code', 'TC 10', 'TC9x Test', 'Tc9x Build', 'Oracle Autotest', 
                        'Database', 'Baseline', 'push_windows', 'push_unix', 'license', 'tcx', 
                        'eng', 'perforce_proxy', 'volume_server', 
                        'Windows Ref Unit Location', 'Unix Ref Unit Location',
                        'Windows RTE Location', 'Unix RTE Location', 'Windows Toolbox Location', 
                        'Unix Toolbox Location', 'UGII License File', 'UGS License Server', 'Unix Dev Units', 
                        'Unix Devop Path', 'Perforce Proxy Path', 'Primary Contact', 'Secondary Contact', 'Num Users']];

I understand rows is a 1xn matrix (which is valid). 我知道rows是一个1xn矩阵(有效)。

Now, I have to change my code, and load the columns from the database. 现在,我必须更改代码,并从数据库中加载列。 Now, suppose all my columns are stores in columns , an array. 现在,假设我所有的列都存储在columns数组中。 How could I create similar matrix as show in rows above? 我如何创建类似于上面各rows所示的矩阵? I wrote the following code: 我写了以下代码:

var rows = [[]];
for(var i = 0; i < columns.length; i++){
    rows[0][i] = columns[i];
}

However, I'm getting the following error: 但是,出现以下错误:

Not a valid 2D array.

Works fine: 工作正常:

var rows = [[]];
var columns = ["a","b","c"];
for(var i = 0; i < columns.length; i++){
  rows[0][i] = columns[i];
}

So what does that error mean? 那么这个错误是什么意思呢? or maybe you need to check your columns Array 也许您需要检查columns Array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM