[英]nodejs execute string variable
I'm using sails.js. 我正在使用sails.js。 1) I'd like to print 'dolphin' from object matchingCol.
1)我想从对象matchingCol打印“海豚”。
var matchingCol = {
'card': {
'add': {
'voo': 'dolphin'
}
}
};
module.exports = {
methodA: function (req, res) {
var a = 'card';
var b = 'add';
var c = 'voo';
return res.send(matchingCol.a.b.c);
},
};
2) If I'd like to declare object, and want to parse 'foo' to key, how can I do? 2)如果我想声明对象,并想解析'foo'键,我该怎么办?
var key = 'foo';
var params = {
key: 'anything'
};
Please help. 请帮忙。
In JavaScript, if you have a variable that represents the property name of an object, you can use square brackets to access it: 在JavaScript中,如果您有一个表示对象属性名称的变量,则可以使用方括号来访问它:
return res.send(matchingCol[a][b][c]);
You can also use square brackets for assignment: 您也可以使用方括号进行分配:
var key = 'foo';
var params = {};
params[key] = 'anything';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.