1

this.MyMenu = function(){
    		this.MyMenu = function(){
                
                
    	for(i = 0; i < vis.Labels.length; i++)
    		{        
              text(vis.Labels[i].name, 100, 50 * i+80);
             
    		}
    	};

I have written a for loop that iterates over the array stored in the Labels property of the vis object, which itself is kept in the global vis variable defined in sketch.js, writing each Labels property to the screen. Mymenu is displayed when I press the space bar and it looks as follows:

Select a Library:

Washington

Moscow

Beijing

I would like, however, that a number of the array was also displayed in the following manner, e.g.

1: Washington

2: Moscow

3: Beijing

I was trying to use IndexOf +1 but with no luck so far. Do you have any tips on how this could be achieved?

Thanks in advance Andy

1
  • In the for loop, i+1 should do. Commented Jun 14, 2020 at 20:28

1 Answer 1

1

You can just simply use template literals to do this

this.MyMenu = function(){                
   for(i = 0; i < vis.Labels.length; i++)
    {        
      text(`${i+1}: ${vis.Labels[i].name}`, 100, 50 * i+80);
    }
};

OR

use string concatenation

this.MyMenu = function(){                
   for(i = 0; i < vis.Labels.length; i++)
    {        
      const index = i+1
      text( (index+ ": " + vis.Labels[i].name) , 100, 50 * i+80);
    }
};

Hope this helps !

Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks Hemant. Much appreciated. I used the second option with string concatenation and it worked like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.