0

I have this below method but when I use variable inside this method it detects it as a string (gives a NaN error). These Variable are chap1, chap2, chap3, chap4 and chap5. How do I fix this.

Thanks

    var chap1 = 0;
    var chap2 = 104;
    var chap3 = 235;
    var chap4 = 309;
    var chap5 = 406;

    ChapterMarkerPlayer.insert({
      container: 'captions-session-player',
      videoId: youtubeId + '?wmode=opaque&theme=light&autohide=0&modestbranding=1&rel=0&autoplay=0',
      width: 469,
      chapters: {
            chap1: '<span style="display: inline-block; width: 88%;">Chapter 1: Evaluating our industry’s track record on innovation</span>',
            chap2: '<span style="display: inline-block; width: 88%;">Chapter 2: Can asset managers learn from top innovators in other industries?</span>',
            chap3: '<span style="display: inline-block; width: 88%;">Chapter 3: How does multi-asset investing align with investor needs?</span>',
            chap4: '<span style="display: inline-block; width: 88%;">Chapter 4: Can investment industry innovators improve investor trust? </span>',
            chap5: '<span style="display: inline-block; width: 88%;">Chapter 5: Will regulators help to improve trust in our industry?</span>'
      }
    });
user3689990
  • 61
  • 2
  • 10

2 Answers2

0

If you want to use variable for property name in object, you should use bracket notation. In your case it will look like this:

var chap1 = 0;
var chap2 = 104;
var chap3 = 235;
var chap4 = 309;
var chap5 = 406;

var chapters = {};
chapters[chap1] = '<span style="display: inline-block; width: 88%;">Chapter 1: Evaluating our industry’s track record on innovation</span>';
chapters[chap2] = '<span style="display: inline-block; width: 88%;">Chapter 2: Can asset managers learn from top innovators in other industries?</span>';
chapters[chap3] = '<span style="display: inline-block; width: 88%;">Chapter 3: How does multi-asset investing align with investor needs?</span>';
chapters[chap4] = '<span style="display: inline-block; width: 88%;">Chapter 4: Can investment industry innovators improve investor trust? </span>';
chapters[chap5] = '<span style="display: inline-block; width: 88%;">Chapter 5: Will regulators help to improve trust in our industry?</span>';

ChapterMarkerPlayer.insert({
    container: 'captions-session-player',
    videoId: youtubeId + '?wmode=opaque&theme=light&autohide=0&modestbranding=1&rel=0&autoplay=0',
    width: 469,
    chapters: chapters
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
0

You cannot use variable as a key while initializing a object. You can use variable as a key after declaring. like

var a = 1;
var b = 2;
var c = {};
c[a] = "a";
c[b] = "b";

You cannot do like

var c = {a : "a", b: "b"}

will gives error;

Md. Yusuf
  • 502
  • 2
  • 6
  • 20