0

I have a variable called current_slide which contains a string called "default"

Now I have a object called document.referencemap:

How can I attach the "default" string from current_slide to the object document.referencemap so that I will get document.referncemap.default ?

What is the best way to attach a string to the object?

At the moment I am calling the object property manually like: document.referncemap.default.

Can anybody give me a hint so I can solve this issue?

My code looks like this

My code

Because the current slide is always changing i need to load the object from the value of current_slide.

Peter B
  • 22,460
  • 5
  • 32
  • 69

2 Answers2

0

use:

document.referencemap[current_slide] = booBar

make sure current_slide is of type string. Read the Property accessors docs for more information.

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
  • I edited my question and added a picture of my code. May that would be more clearer. I want to replace the welcome (which i marked yellow) with the result of current_slide.slide – Stark Toni Sep 04 '17 at 08:40
  • Than use `delete document.referencemap.welcome` and after that assign the new property with the bracket notation if I get you right what you want to achieve – Kristianmitk Sep 04 '17 at 08:51
  • No that not exactly what i wanted I wanted to Dynamically access object property using variable I found a solution here https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable – Stark Toni Sep 04 '17 at 09:00
0

You can use this:

temp0 = document.referencemap[current_slide.slide];

Explanation:
There are two ways to get values from Javascript objects,

  1. using property notation: temp0 = document.referencemap.welcome;
  2. using dictionary notation: temp0 = document.referencemap["welcome"];

This will get the exact same item from the object. Note however that 2. uses a string key. Because of that you can also use a variable that contains a string, in this case: temp0 = document.referencemap[current_slide.slide];

Peter B
  • 22,460
  • 5
  • 32
  • 69