【问题标题】:creating a dynamic binding in JavaScript在 JavaScript 中创建动态绑定
【发布时间】:2012-07-26 22:44:38
【问题描述】:

我正在实现一项功能,该功能允许我将列动态添加到JavaScript 表中:

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 = [[]] 保存表数据...info[[]] 包含从数据库查询的JSON 对象。 这段代码的问题:

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

我正在尝试将colinfo 的某些属性动态绑定。但我不知道是否可能......我怎么能这样做? 假设用户添加了一个新列 TC_12。因此,我不知道TC_12 存在,所以我想动态地将col 出价到info[i],这样它就可以以某种方式让我产生info[i].TC_12。 有什么想法吗?

【问题讨论】:

标签: javascript dynamic-binding


【解决方案1】:

使用方括号表示法将变量的值或其他表达式的结果用作对象属性。

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

仅供参考,您可以通过传递多个参数来一次调用.push() 来完成所有这些推送...

    temp.push(parseInt(info[i].site_id),
              info[i].site,
              info[i].site_code,
              processArray(info[i].tc10),
              processArray(info[i].tc9x_test),
              // etc...
             );

【讨论】:

  • 听起来不错,但它没有给出2-D 矩阵......所以谷歌可视化 API 抱怨......
  • @philippe:不知道为什么会这样。
猜你喜欢
  • 1970-01-01
  • 2011-10-08
  • 2020-04-12
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多