Search code examples
javascriptnode.jsloopsfor-loopvar

Multiple FOR var loops that read from config Issue


I'm setting up my code and having an issue.. This code is just an example what i'm trying to do. SO basically config.Te[i].ref should read all the ref in Te.. but it only reads the first row instead of all rows in Te...

regards

for (var i in config.acc,config.Te,config.Hu,config.Ve) 

                console.log(config.Te[i].ref)
          {}

when i change the log output to config.acc[i].ref or other, it also reads the first row only of that specific config.

but when i change it to for (var i in config.acc) it reads all the ref...

so i think i formatted the code wrong, and i dont know how to fix it. the error must be in seperating the configs. i just want to call them seperatly 1 by 1.

"acc":
            [
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 },
            {"type":"PIR",      "ref":0 }
            ],


Solution

  • You can store your main objects in array and then use nested for loop.

    const arr = [config.acc,config.Te,config.Hu,config.Ve]
    for(let elm of arr){
       for(let i in elm){
          console.log(elm[i].ref)
       }
    }