I am trying to add a key value pair to a javascript object, except the key is a variable representing something else.
See the code for what I've tried
This is my code so far:
var temp = 3;
var dictionary = {};
dictionary["temp"] = 4;
console.log(dictionary);
When I do console.log(dictionary)
I am getting {temp: 4}
, when I actually want {3:4}
. How should I go about this so that the key added is what temp actually represents. I know I could've just done dictionary.3 = 4
, but answering this question (using the temp variable) will help me solve a larger, more complex problem.