javascript key as variable code example
Example 1: use value of variable as key javascript
var final = [{
id: 23477234
}];
//add new object to the array using variables as keys AND values
var newArrray = {};
var key = "name";
var value = "John";
newArrray[key] = value;
final.push(newArrray);
/*
final now equals
[{
id: 23477234,
name: "John"
}]
*/
Example 2: how to use variable in js key
ES6 defines ComputedPropertyName
var thetop = "top",
obj = { [thetop]: 10 };
console.log(obj.top); // -> 10