Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript create object key from variable

let yourKeyVariable = "objectKeyName";

//For ES6 and Babel
{
    [yourKeyVariable]: "yourValue",
}

// ES5 Alternative
// Create the object first, then use [] to set your variable as a key
var yourObject = {};

yourObject[yourKeyVariable] = "yourValue";

// RESULT:
// {
//   "objectKeyName": "yourValue"
// }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #create #object #key #variable
ADD COMMENT
Topic
Name
8+4 =