1

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'}]
0

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.