I have code like this.
var key = \"anything\";
var object = {
key: \"key attribute\"
};
I want to know if there is a way to rep
Well, there isn't a "direct" way to do this...
but this should do it:
json[key] = json.key;
json.key = undefined;
Its a bit tricky, but hey, it works!
Recently needed a solution how to set cookies passing the dynamic json key values. Using the https://github.com/js-cookie/js-cookie#json, it can be done easily. Wanted to store each selected option value of user in cookie, so it's not lost in case of tab or browser shutting down.
var json = {
option_values : {}
};
$('option:selected').each(function(index, el) {
var option = $(this);
var optionText = option.text();
var key = 'option_' + index;
json.option_values[key] = optionText;
Cookies.set('option_values', json, { expires: 7 } );
});
Then you can retrieve each cookie key value on each page load using
Cookies.getJSON('option_values');