-1

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.

Morphyish
  • 3,932
  • 8
  • 26
user11508332
  • 537
  • 1
  • 11
  • 28

1 Answers1

0

You need to change the variable "temp" to dynamic temp(get rid of the double quotes).

var temp = 3; 
var dictionary = {};

dictionary[temp] = 4;
console.log(dictionary);
Avanthika
  • 3,984
  • 1
  • 12
  • 19