如果语句不使用JSON阵列工作
我有2不和谐的客户端ID`{一个JSON文件
{
"premium": [
"a random string of numbers that is a client id",
"a random string of numbers that is a client id"
]
}
我曾经试图访问这些客户机ID做使用for循环+ if语句在程序中的事情:
for(i in premium.premium){
if(premium.premium[i] === msg.author.id){
//do some stuff
}else{
//do some stuff
当程序运行时,它运行for循环,去到别的第一和运行在那里的代码(不应该发生的),然后运行的代码,如果两次。但只有2个客户端ID和for循环已经跑了3次,运行时,它在第一时间推移即刻即使谁发送消息的人在JSON文件中的客户端ID的人。
我怎样才能解决这个问题?任何帮助是极大的赞赏。
回答如下:您可能需要您的for
循环中添加一个return语句。否则,循环将继续运行,直到一个条件已经满足,或者它有没有别的遍历。见documentation on for loops here。
例如,这里是没有return
语句:
const json = {
"premium": [
"aaa-1",
"bbb-1"
]
}
for (i in json.premium) {
if (json.premium[i] === "aaa-1") {
console.log("this is aaa-1!!!!")
} else {
console.log("this is not what you're looking for-1...")
}
}