简体   繁体   中英

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)

var columns = ['D'];

("SELECT ?? FROM money",[columns], function (err, results, fields) 

IN THIS I HAVE TO SPECIFY COLUMN NAME(D) --

results[2].D

OR

var x = String("D") + i.toString();

results[2].x

THIS DIDT WORKED

If you want to access a column by name, but you have the name in a variable, remember JavaScript objects allow access that way:

let column = 'D';
results[2][column] // => D column value

Your var x defines an x variable. This, however:

results[2].x

This looks up a property named literally x on that result.

It's also not clear what the intent behind String("D") is, as "D" is already a string. Making a superstring?

JavaScript has an interpolation system specifically for this:

var x = `D${i}`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM