如何将 JSON 文件中的数字递减到 0?
How to decrement numbers in JSON file until 0?
我有一个 JSON 文件,它有针对特定工作人员的静音
我想减少它直到 0
我该怎么做?
我还想要一个代码来减少所有员工的静音
client.on('message', message => {
if(!staffstats[message.author.id]) staffstats[message.author.id] = {
mutes: 0,
bans: 0,
warns: 0,
tickets: 0,
appeals: 0,
vips: 0,
WarnedTimes: 0
}
if(message.content === prefix + "mutes-reset"){
user = message.mentions.users.first();
staffstats[user.id].mutes--;
}
})
你很接近!您可以 staffstats[user.id].mutes = staffstats[user.id].mutes - 1;
,但是,您确实要求 直到 0,因此在更改值之前进行简单检查就足够了:
if (!staffstats[user.id].mutes <= 0) //if mutes value is NOT lower or equal to 0, do:
staffstats[user.id].mutes = staffstats[user.id].mutes - 1; //reduces current value of mutes by 1
decrement all staff members mutes
,您需要知道工作人员是谁,以及他们的 ID。假设您知道这一点,您可以循环遍历用户 ID 数组。
如果您仅将所有员工的所有值存储在对象中 ({}
),那么您可以对所有键(即用户 ID)执行 Object.keys(staffstats);
,因为它很方便在一个你可以循环的数组中。
var staffId = ['12345', '23456', '34567']; //this is just an example array
staffId.forEach(id => { //loop through array of staffId, storing value in id variable
//same method as above
if (!staffstats[id].mutes <= 0)
staffstats[id].mutes = staffstats[id].mutes - 1;
};
我有一个 JSON 文件,它有针对特定工作人员的静音 我想减少它直到 0 我该怎么做?
我还想要一个代码来减少所有员工的静音
client.on('message', message => {
if(!staffstats[message.author.id]) staffstats[message.author.id] = {
mutes: 0,
bans: 0,
warns: 0,
tickets: 0,
appeals: 0,
vips: 0,
WarnedTimes: 0
}
if(message.content === prefix + "mutes-reset"){
user = message.mentions.users.first();
staffstats[user.id].mutes--;
}
})
你很接近!您可以 staffstats[user.id].mutes = staffstats[user.id].mutes - 1;
,但是,您确实要求 直到 0,因此在更改值之前进行简单检查就足够了:
if (!staffstats[user.id].mutes <= 0) //if mutes value is NOT lower or equal to 0, do:
staffstats[user.id].mutes = staffstats[user.id].mutes - 1; //reduces current value of mutes by 1
decrement all staff members mutes
,您需要知道工作人员是谁,以及他们的 ID。假设您知道这一点,您可以循环遍历用户 ID 数组。
如果您仅将所有员工的所有值存储在对象中 ({}
),那么您可以对所有键(即用户 ID)执行 Object.keys(staffstats);
,因为它很方便在一个你可以循环的数组中。
var staffId = ['12345', '23456', '34567']; //this is just an example array
staffId.forEach(id => { //loop through array of staffId, storing value in id variable
//same method as above
if (!staffstats[id].mutes <= 0)
staffstats[id].mutes = staffstats[id].mutes - 1;
};