3

I have a variable like

var column = $(this).attr('class');

I then need to add this variable as the name of a JSON object like so

obj.push({ column : anotherVar });

This outputs "column" instead of my variable. What is the easiest way to convert my variable into a usable string in JSON.

1
  • I don't understand your question. Please elaborate. Commented Nov 1, 2010 at 14:30

1 Answer 1

6

You have to do it in two steps:

var tmp = {}; tmp[column] = anotherVar;
obj.push(tmp);

You can always use [] to refer to object properties whose names are dynamic, but you can't use such names in an object literal.

Sign up to request clarification or add additional context in comments.

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.