set object key with variable javascript code example
Example 1: variable key name js
var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
Example 2: js variable for key obj
{
[yourKeyVariable]: someValueArray,
}
Example 3: js change key value in object
var object = { boo:true, baa:5 };
console.log(object);
function change() {
object.boo = false;
object.baa++;
};
change();
console.log(object);
Example 4: javascript set object key as variable
let variableKey = "exampleKey"
let variableValue = "exampleValue"
let data = {
[variableKey] : variableValue
}
console.log(data)