0
for (var i = 0; i < vms.length; i++){
    for (var [key, value] of Object.entries(vms[i])){
        System.log(key, value);
    }
}

So here is my code, knowing that "vms" is a list of dictionaries. I'm getting the "missing ; after for-loop initializer" error and I don't really know why. I've already been searching here for some answers and the only answers I saw was to use var but I already do...

nasci
  • 1
  • 1
  • 3
    What's the environment you're running this in? The code as shown is fine, although I don't know what `System` is. – robertklep Jul 04 '22 at 08:47
  • It would be useful to point out if it was the first or second `for` on which you are having trouble. – Ken Y-N Jul 04 '22 at 08:50
  • I'm writing this code in a Script on vRealize Orchestrator, to answer the second question the error points me on the first line. – nasci Jul 04 '22 at 08:54
  • If this is Node.js, what's the version? Try to log `process.versions` – qrsngky Jul 05 '22 at 06:09
  • @nasci Does that environment support ES6? It looks like it doesn't understand `for … of` syntax. And most likely `Object.entries` won't work either – Bergi Jul 05 '22 at 06:48
  • @Bergi My interpreter is actually not a Browser, but an ESX server so this might be problem. As you said, I think he doesn't understand Object.entries. So I will try different ways to see if i can work around the problem. – nasci Jul 05 '22 at 07:34
  • No, the syntax error comes from not understanding the `of` keyword. `Object.entries` will cause an "entries is not a method" or "undefined is not a function" error. – Bergi Jul 05 '22 at 07:37
  • @nasci What is an "ESX server"? Can you link its docs and/or add the appropriate tags to your question? – Bergi Jul 05 '22 at 07:40
  • @Bergi You were right I just tried a simple for...of and it didn't worked.. So the problem is in the for...of. Now for the documentation check this link. Hope it will help, sorry for not beeing that precise I'm still learning all this. [link](https://docs.vmware.com/en/VMware-vSphere/index.html) – nasci Jul 05 '22 at 08:09

1 Answers1

-1

Only the array have length property not dictionaries in javascript. Dictionary is considered as an object. You can use below code to iterate the dictionary key value pair.

for (var [key, value] of Object.entries(vms)){
console.log(key, value);
}
  • But "vms" is actually an Array of dictionaries. You can see how it looks here at the very bottom. https://rokett.github.io/posts/convert-powershell-objects-to-javascript-objects-in-vrealize-orchestrator/ – nasci Jul 05 '22 at 06:46