我正在尝试使用package.json将变量存储在多个“文件夹”中,例如:
"servers": {
"folder1": {
"modrole": "Moderator"
},
"folder2": {
"modrole": "bot owner"
}
}
我要做的是用一个变量替换文件夹规范:
var foldername = 'folder1'
package.servers.[value of foldername].modrole
有什么办法吗?
答案 0 :(得分:1)
使用方括号内的变量。
const package = {
"servers": {
"folder1": {
"modrole": "Moderator"
},
"folder2": {
"modrole": "bot owner"
}
}
}
let folderName = "folder1"
console.log(package.servers[folderName].modrole)