[英]I want to replace column name with variable in the syntax results[3].column_name , so that i can print according to the user
I am trying to print a specific column of a mysql table told by the user in runtime (without using if else ,which will make code tidious) 我正在尝试在运行时打印由用户告诉的mysql表的特定列(不使用if else,这会使代码很麻烦)
var columns = ['D'];
("SELECT ?? FROM money",[columns], function (err, results, fields)
IN THIS I HAVE TO SPECIFY COLUMN NAME(D) -- 在此我必须指定列名称(D)-
results[2].D
OR 要么
var x = String("D") + i.toString();
results[2].x
THIS DIDT WORKED 这个DIDT工作
If you want to access a column by name, but you have the name in a variable, remember JavaScript objects allow access that way: 如果您想按名称访问列,但是名称中包含变量,请记住JavaScript对象允许通过这种方式访问:
let column = 'D';
results[2][column] // => D column value
Your var x
defines an x
variable. 您的
var x
定义了一个x
变量。 This, however: 但是,这是:
results[2].x
This looks up a property named literally x
on that result. 这会在该结果上查找一个名为
x
的属性。
It's also not clear what the intent behind String("D")
is, as "D"
is already a string. 还不清楚
String("D")
的意图是什么,因为"D"
已经是字符串。 Making a superstring? 做一个超弦乐?
JavaScript has an interpolation system specifically for this: JavaScript具有专门用于此的插值系统:
var x = `D${i}`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.