Skip to content Skip to sidebar Skip to footer

How Can I Convert A String To A Variable Name In Node.js?

//Admin.js var insertAdminFeed = function(s, id, timestamp){ var admin_att_new_key = '12345'; var admin_att_new_key2 = 'abc'; var admin_att_new_key3 = 'zyzyz'; va

Solution 1:

This is not really possible in JavaScript.

You'd usually use an object literal to achieve similar needs.

var key = 'foo';
obj[key] = 1;
obj['foo'];

To be thorough, it is technically possible in JS using eval. But really, don't do this.

eval("var "+ name + " = 'some value';");
eval("console.log("+ name  +")");

Post a Comment for "How Can I Convert A String To A Variable Name In Node.js?"