根据列标题动态创建变量名



假设一个google表的列名,PERSON_ID,通过代码检索。

如何动态创建一个具有此名称的变量?我的设想是这样的:

var columnName='PERSON_ID';
var [columnName]=''

希望大家都明白了。

这里有一个例子,我相信你正在努力完成:

const variables = {}; // create an empty object and name it "variables".
const ranStr_1 = "ABC";
const ranStr_2 = "XYZ";
// set the string "ABC" and "XYZ" stored in variable 'ranSTR_1' and 'ranSTR_2' as keys inside the Object 'variables', with paired values "value 1" and "value 2".
variables[ranStr_1] = "value 1"; 
variables[ranStr_2] = "value 2";
console.log(variables.ABC) // should shows "value 1" in console.
console.log(variables["ABC"]) // should shows "value 1" in console.
console.log(variables.XYZ) // should shows "value 2" in console.
console.log(variables["XYZ"]) // should shows "value 2" in console.
// you can also do something like this:
const varNames = ["name_1","name_2","name_3","name_4"];
for (const [i,key] of varNames.entries()) {
variables[key] = i
}
// shows all stored key: value pairs in console:
console.log(variables)

相关内容

  • 没有找到相关文章

最新更新