使用字符串读取配置文件变量
Use String to Read Config File Variable
我有一个如下所示的配置文件 (leaderboard.json):
{
"usercount<@386679122614681600>": 0
}
我正在尝试在我的 index.js 文件中读取该变量的值(因此它将为 0),该文件如下所示:
var LEADERBOARD = require('./leaderboard.json');
const usercount = 'usercount'+user
var countvalue = LEADERBOARD.usercount
console.log(countvalue)
我基本上希望 countvalue 为 return 0,但我不能简单地使用 LEADERBOARD.usercount<@386679122614681600>,因为该名称将会更改。
基本上,我想知道是否有解决方法可以使用我创建的字符串读取我的配置文件变量。
为此你需要使用 bracket notation。
var countvalue = LEADERBOARD[usercount]
这是一个例子:
const foo = {
bar123: 'baz'
}
const prop = 'bar' + 123
console.log(foo[prop])
使用 []
表示法。
因为 .
符号 属性 必须是有效的 JS 标识符 Read here.
property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object. is valid, while object.1 is not.
let obj = {
"usercount<@386679122614681600>": 0
}
const count = `386679122614681600`;
const useCount = `usercount<@${count}>`;
console.log(obj[useCount])
使用您创建的字符串您可以return用户帐户的值。
const LEADERBOARD = {
"usercount<@386679122614681600>": 0
}
// String you create
const userId = '386679122614681600'
const userCount = `usercount<@${userId}>`
console.log(LEADERBOARD[userCount]) // 0
如果您知道您只访问第一个元素,这个答案将对您有所帮助!没有关于此的更多信息,所以我认为这是一个潜在的答案:
您可以使用 Object.keys 将值放入数组格式,然后使用 [0] 访问第一个值,它将 return 为 0 值。
var obj = {
"usercount<@386679122614681600>": 0
}
var countvalue = obj[Object.keys(obj)[0]];
console.log(countvalue)
我真的希望这对您有所帮助。如果这不是您的要求,请告诉我。
我有一个如下所示的配置文件 (leaderboard.json):
{
"usercount<@386679122614681600>": 0
}
我正在尝试在我的 index.js 文件中读取该变量的值(因此它将为 0),该文件如下所示:
var LEADERBOARD = require('./leaderboard.json');
const usercount = 'usercount'+user
var countvalue = LEADERBOARD.usercount
console.log(countvalue)
我基本上希望 countvalue 为 return 0,但我不能简单地使用 LEADERBOARD.usercount<@386679122614681600>,因为该名称将会更改。
基本上,我想知道是否有解决方法可以使用我创建的字符串读取我的配置文件变量。
为此你需要使用 bracket notation。
var countvalue = LEADERBOARD[usercount]
这是一个例子:
const foo = {
bar123: 'baz'
}
const prop = 'bar' + 123
console.log(foo[prop])
使用 []
表示法。
因为 .
符号 属性 必须是有效的 JS 标识符 Read here.
property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object. is valid, while object.1 is not.
let obj = {
"usercount<@386679122614681600>": 0
}
const count = `386679122614681600`;
const useCount = `usercount<@${count}>`;
console.log(obj[useCount])
使用您创建的字符串您可以return用户帐户的值。
const LEADERBOARD = {
"usercount<@386679122614681600>": 0
}
// String you create
const userId = '386679122614681600'
const userCount = `usercount<@${userId}>`
console.log(LEADERBOARD[userCount]) // 0
如果您知道您只访问第一个元素,这个答案将对您有所帮助!没有关于此的更多信息,所以我认为这是一个潜在的答案:
您可以使用 Object.keys 将值放入数组格式,然后使用 [0] 访问第一个值,它将 return 为 0 值。
var obj = {
"usercount<@386679122614681600>": 0
}
var countvalue = obj[Object.keys(obj)[0]];
console.log(countvalue)
我真的希望这对您有所帮助。如果这不是您的要求,请告诉我。