Dalton Gore
Dalton Gore

Reputation: 919

Accessing Object Elements via For Loop

Okay, so I've tired quite a few things. And I'm still boggled, and I know the answer is going to be something ridiculously simple, but I have to ask anyway.

I have a function:

Module.load = function(a) {
  require("./modules/"+a+".js");
  Module.loaded.push(a);
  Log("Loaded Module: "+a); 
};

And using Module.load('basic'); loads the basic file. Now, I'm defining modules via an object:

Modules = { basic:1,queue:0,admin:1,notify:0 }

So I wrote this function:

for (x in Modules) { if (Modules[x] == 1) Module.load(x); };

But it's not working, and for the life of me, I can't figure out why.

Upvotes: 2

Views: 76

Answers (1)

rid
rid

Reputation: 63442

The code works:

var Module = {}; Module.load = function(a) {
  console.log("Loaded Module: "+a); 
};

var Modules = { basic:1,queue:0,admin:1,notify:0 }

for (var x in Modules) { if (Modules[x] == 1) Module.load(x); };
Loaded Module: basic
Loaded Module: admin

Therefore the problem must be elsewhere. Make sure that:

  • you don't have any syntax error
  • Module.loaded exists and is an array
  • require is defined as a function and doesn't crash
  • Log is defined as a function and doesn't crash

Upvotes: 4

Related Questions