1

I am building an array of Strings in my Groovy code and want to pass the array to a JavaScript script contained in my code. However, Groovy doesn't put the strings in the array into quotes as Javascript expects. I'm passing the array using GStrings:

var strains = ${strainNames};

JavaScript evaluates it to this:

var strains = [pseudomonas_aeruginosa_D421, pseudomonas_aeruginosa_OC2E, pseudomonas_aeruginosa_EA0A, pseudomonas_aeruginosa_0812];

Is there a way to add quotes to each element or do I already have to add them in my Groovy code?

2 Answers 2

2

You could change:

var strains = ${strainNames};

to

var strains = ${strainNames.inspect()};
Sign up to request clarification or add additional context in comments.

2 Comments

That gives me the same array without quotes.
FWIW, for me, @tim_yates' answer works as expected (adding quotes to the array elements), using Groovy v3.0.11. From the .inspect docs, .inspect() returns a String that matches what would be typed into a terminal to create this object, e.g. [1, 'hello'].inspect() -> [1, 'hello']
0

You could try to include it as a string and transform it to an array:

var strains = "${strainNames}"
  .slice(1, -1)  // Remove brackets
  .split(", ");   // Split at separators

Comments

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.