Javscript string to Ivalue conversion

I need to assign a variable the following:

ref => { this.marker = ref}

This is happening in a loop and i need this.marker to be named like this.marker + i, so i can set and access this.marker1, this.marker2 etc..

Is this possible?

1 answer

  • answered 2018-01-11 21:11 Andrew Taylor

    You need to index into the object by string:

    this['marker' + i] = ref

    In Javascript, a.b and a['b'] are the same, except that in the latter, 'b' can be replaced with any expression that results in a string (or indeed any value that Javascript is willing to treat as a string, which is anything at all).

    That said, you might be better off with an array depending on your use case.