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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With