简体   繁体   English

我想在语法result [3] .column_name中用变量替换列名,以便我可以根据用户进行打印

[英]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.

相关问题 如何在 mysql / express 中使用变量作为列名? - How can I use a variable as column name in mysql / express? 续集:列“<column_name> " 不存在 (Postgresql)</column_name> - Sequelize: column "<column_name>" does not exist (Postgresql) 如何隐藏变量名称的jqGrid列? - How do I hide a jqGrid column of variable name? 我想在 UI 中只向用户显示 3 列,但想将所有表数据导出到 excel,那么如何根据 header 文本隐藏列? - I want to show the user only 3 columns in UI, But want to Export all table data to excel, So how can I hide the column based on the header text? 如何从DML语句获取column_name和table_name? - How to get column_name and table_name from DML statement? 在函数中绑定一个变化的变量,我想要一个名字 - Bind a changing variable in function, I want a name 有没有可能我在表名 users 上搜索,然后在 mvc 中的用户表名角色列上进行搜索 - Is there any possibility that i search on the table name users and then specifically on the column of user table name role in mvc 如何根据名称对列进行排序,升序和降序 - How to sort column according to name, ascending and descnding 我只想从数组中打印第一个值(名称) - i just want to print the first value(name) from the array 用户单击按钮时如何存储类名称,以便下次访问时可以将其用作默认名称? - How can I store a class name when a user clicks on a button so it can be used as a default on next visit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM