2

I have a list of 3 lines. I want to efficiently separate each line out as it’s own individual item.

myList = [MultiLine.attributes.renderers[0], MultiLine.attributes.renderers[1],
MultiLine.attributes.renderers[2]]

What I want:

    line1 = MultiLine.attributes.renderers[0];
    line2 = MultiLine.attributes.renderers[1];
    line3 = MultiLine.attributes.renderers[2];

I know in Python there is the casting function which I would use like so:

line + str(i) = MultiLine.attributes.renderers[i];

However I'm getting ReferenceError: invalid assignment left-hand side with the equivalent JS:

for(var j = 0; j < myList.length; j++){
    "line" + String(j) = MultiLine.attributes.renderers[j];
    }

Any ideas?

I don't know how to properly go about changing the value of my variable within the for-loop. Would Break statements help?

Ally Alfie
  • 131
  • 11
  • 1
    1) You can't have dynamic variable names in Javascript. 2)There is no need to make variables whose names contain a sequence line `line1,line2,line3` simply create an array and access them using indexes. – Maheer Ali Jun 05 '19 at 11:02

1 Answers1

0

You could use a destructuring assignment with the array.

var [line1, line2, line3] = MultiLine.attributes.renderers;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392