0

I have a JSON file of 2 discord client IDs `{

{
       "premium": [
         "a random string of numbers that is a client id",
         "a random string of numbers that is a client id"
         ]
}

I have tried to access these client IDs to do things in the program using a for loop + if statement:

for(i in premium.premium){
      if(premium.premium[i] === msg.author.id){
        //do some stuff
      }else{
       //do some stuff

When the program is ran, it runs the for loop and goes to the else first and runs the code in there (not supposed to happen), then runs the code in the if twice. But there are only 2 client IDs and the for loop has ran 3 times, and the first time it runs it goes instantly to the else even though the person who sent the message has their client ID in the JSON file.

How can I fix this? Any help is greatly appreciated.

  • Can you provide some more context? it's a little bit difficult to help with this scenario without more context. – mralanlee Feb 09 '19 at 23:58
  • Possibly relevant: [Why is using “for…in” with array iteration a bad idea?](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Jonathan Lonowski Feb 10 '19 at 00:20

3 Answers3

0

You may want to add a return statement within your for loop. Otherwise, the loop will continue running until a condition has been met, or it has nothing else to loop over. See the documentation on for loops here.

For example, here it is without return statements:

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...")
  }
}

And here it is with return statements:

const json = {
  "premium": [
    "aaa-2",
    "bbb-2"
  ]
}

function loopOverJson() {
for (i in json.premium) {
  if (json.premium[i] === "aaa-2") {
    console.log("this is aaa-2!!!!")
    return
  } else {
    console.log("this is not what you're looking for-2...")
    return
  }
}
}

loopOverJson()

Note: without wrapping the above in a function, the console will show: "Syntax Error: Illegal return statement."

Ethan Ryan
  • 467
  • 10
  • 16
0
for(i in premium.premium){
   if(premium.premium[i] === msg.author.id){
      //do some stuff
   } else{
       //do some stuff
   }
}

1) It will loop through all your premium.premium entries. If there are 3 entries it will execute three times. You could use a break statement if you want to exit the loop once a match is found.

2) You should check the type of your msg.author.id. Since you are using the strict comparison operator === it will evaluate to false if your msg.author.id is an integer since you are comparing to a string (based on your provided json).

Use implicit casting: if (premium.premium[i] == msg.author.id)
Use explicit casting: if (premium.premium[i] === String(msg.author.id))

0

The really fun and easy way to solve problems like this is to use the built-in Array methods like map, reduce or filter. Then you don't have to worry about your iterator values.

eg.

const doSomethingAuthorRelated = (el) => console.log(el, 'whoohoo!');

const authors = premiums
                 .filter((el) => el === msg.author.id)
                 .map(doSomethingAuthorRelated);

As John Lonowski points out in the comment link, using for ... in for JavaScript arrays is not reliable, because its designed to iterate over Object properties, so you can't be really sure what its iterating on, unless you've clearly defined the data and are working in an environment where you know no other library has mucked with the Array object.

4m1r
  • 12,234
  • 9
  • 46
  • 58