how to use variable as key in javascript object code example

Example 1: variable key name js

//You need to make the object first, then use [] to set it.

var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);

Example 2: javascript create object key from variable

//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";

Example 3: javascript variable as object key

var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);

Example 4: js variable for key obj

{
    [yourKeyVariable]: someValueArray,
}

Example 5: how to use variable in js key

ES6 defines ComputedPropertyName
var thetop = "top",
    obj = { [thetop]: 10 };

console.log(obj.top); // -> 10