how to have a variable key value in javascript code example
Example 1: how to use variable in js key
ES6 defines ComputedPropertyName
var thetop = "top",
obj = { [thetop]: 10 };
console.log(obj.top); // -> 10
Example 2: 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"
}]
*/