Reputation: 23297
I'm trying out the node-mysql module on node js. What I want to do is to be able to come up with an object like this:
{'room1':'Room 1','room2':'Room 2','room3':'Room 3'}
Here's the code:
var boom = results;
var rooms = [];
var index = 0;
var name = 'session';
for(var b in boom){
var ses = name + index;
rooms[b] = {ses : boom[b]['ses_title']};
index++;
}
The ses variable is being treated as a string in the code above. And I end up with something like this:
[{ses : 'class session'} , {ses : 'team session'}]
Upvotes: 1
Views: 439
Reputation: 122906
You can't assign key names like that. Use bracket notation and it should work:
var boom = results;
var rooms = [];
var index = 0;
var name = 'session';
for(var b in boom){
rooms[b] = {};
rooms[b][name + index] = boom[b]['ses_title']};
index += 1;
}
Upvotes: 2
Reputation: 154494
You'll need to use something like:
rooms[b] = {};
rooms[b][ses] = boom[b]['ses_title'];
This is because keys in object literals are always interpreted literally, not evaluated.
Upvotes: 2