Javascript从对象的值中删除双引号

时间:2017-01-06 14:31:21

标签: javascript code-generation

我在Java类中生成JavaScript对象以便在javascript中使用,结果如下所示:

var gridDefinition      = {"width":"100%",
"height":700,
"sortable":true,
"columns"[{"datafield":"id","datatype":"string","width":300,"hidden":true,"text":"ID"},
{"datafield":"lastname","datatype":"string","cellsrenderer":"renderer_openEntry","width":300,"text":"Nachname"},
{"datafield":"firstname","datatype":"string","width":200,"text":"Vorname"},
{"datafield":"officePhoneNumber","datatype":"string","width":150,"text":"Telefon"},
{"datafield":"companyName","datatype":"string","width":300,"text":"Firma"},
{"datafield":"mailServer","datatype":"string","width":200,"text":"Mail-Server"},
{"datafield":"mailFile","datatype":"string","width":400,"text":"Mail-Datei"}]} ;

cellsrenderer是回调函数名称。如何删除renderer_link周围的双引号?结果行应如下所示:

{"datafield":"lastname","datatype":"string","cellsrenderer":renderer_openEntry,"width":300,"text":"Nachname"},

1 个答案:

答案 0 :(得分:1)

如何删除引号"是一个错误的问题。在代码运行时,没有可以删除的引号。

您需要的是一个将名称映射到变量的地图,例如

var myFunctions = {
  renderer_openEntry: renderer_openEntry,
  // other functions
};

然后处理对象以更新所有cellsrender proeprties的值以引用地图中的值而不是字符串:

gridDefinition.columns.forEachfunction(column) {
  if (column.cellsrenderer) {
    column.cellsrenderer = myFunctions[column.cellsrenderer];
  }
});