Is there a way to use variable keys in a JavaScript object literal?

Javascript

Javascript Problem Overview


I have code like this.

var key = "anything";   
var object = {   
    key: "key attribute"  
};

I want to know if there is a way to replace that key with "anything".

like

var object = {  
    "anything": "key attribute"     
};

Javascript Solutions


Solution 1 - Javascript

In ES6, use computed property names.

const key = "anything";   

const object = {   
    [key]: "key attribute"
//  ^^^^^  COMPUTED PROPERTY NAME
};

Note the square brackets around key. You can actually specify any expression in the square brackets, not just a variable.

Solution 2 - Javascript

Yes. You can use:

var key = "anything";
var json = { };
json[key] = "key attribute";

Or simply use your second method if you have the values at hand when writing the program.

Solution 3 - Javascript

On modern Javascript (ECMAScript 6) you can sorround the variable with square brackets:

var key = "anything";

var json = {
    [key]: "key attribute"
};

Solution 4 - Javascript

This should do the trick:

var key = "anything";

var json = {};

json[key] = "key attribute";

Solution 5 - Javascript

Solution:

var key = "anything";

var json = {};

json[key] = "key attribute";

Solution 6 - Javascript

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');

Solution 7 - Javascript

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!

Solution 8 - Javascript

Closures work great for this.

function keyValue(key){
  return function(value){
    var object = {};
    object[key] = value;
    return object;
  }
}

var key = keyValue(key);
key(value);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGordian YuanView Question on Stackoverflow
Solution 1 - Javascriptuser663031View Answer on Stackoverflow
Solution 2 - JavascripttvanfossonView Answer on Stackoverflow
Solution 3 - JavascriptJorge BarrosoView Answer on Stackoverflow
Solution 4 - JavascriptD. EvansView Answer on Stackoverflow
Solution 5 - JavascriptAatif BandeyView Answer on Stackoverflow
Solution 6 - JavascriptAlexey VorobyovView Answer on Stackoverflow
Solution 7 - JavascriptjrharshathView Answer on Stackoverflow
Solution 8 - JavascriptRickView Answer on Stackoverflow