Medallyon
Medallyon

Reputation: 120

Creating JSON Attributes

Good Day stackOverflow,

I'm having a little trouble creating JSON objects. I have this snippet of code that's supposed to add names to a list called members, and then add a property to each of those names. This is the the snippet in question:

for (var i = 0; i < msg.channel.server.members.length; i++) {
  savedVars.Servers[thisServerID]["Members"].push(msg.channel.server.members[i].name);
  savedVars.Servers[thisServerID]["Members"][i]["Object"] = {};
};

The above snippet writes the list of names perfectly, but doesn't create the "Object" attribute. There is also no error or any indication that it processes this line at all.

The structure overall should look like this:

Members: [
  Member1: {
    Object: {}
  },
  Member2: {
    Object: {}
  },
  etc...
]

Many Thanks in Advance. Yours,
@Medallyon

Upvotes: 2

Views: 54

Answers (1)

George Kagan
George Kagan

Reputation: 6124

Your first line pushes a string to the array, and then you use it as an object to add another property, it fails silently because the browser thinks you're accessing the string's characters.

Just use this:

savedVars.Servers[thisServerID]["Members"].push({
    name: msg.channel.server.members[i].name,
    object: {}
});

Upvotes: 1

Related Questions