javascript use variable as key code example
Example 1: variable key name js
var key = "happyCount";
var obj = {};
obj[key] = someValueArray;
myArray.push(obj);
Example 2: javascript create object key from variable
{
[yourKeyVariable]: "yourValue",
}
var yourObject = {};
yourObject[yourKeyVariable] = "yourValue";
Example 3: how to change input value in javascript using class
document.querySelector("form[name='foo'] > input.bar").value = "hehe works"
Example 4: use value of variable as key javascript
var final = [{
id: 23477234
}];
var newArrray = {};
var key = "name";
var value = "John";
newArrray[key] = value;
final.push(newArrray);
Example 5: how to use variable in js key
ES6 defines ComputedPropertyName
var thetop = "top",
obj = { [thetop]: 10 };
console.log(obj.top);
Example 6: how to assign same value of a key to 2 different keys in output JSON jolt
[
{
"operation": "shift",
"spec": {
"survey": {
"key": {
"@": [ "a.b.firstProperty", "a.b.secondProperty" ],
"#Constant": [ "a.b.thirdProperty", "a.b.fourthProperty" ]
}
}
}
}
]