How to create GWT JsArray?

Prince picture Prince · Jun 17, 2012 · Viewed 7.1k times · Source

I need to convert values of type T into JsArray.

For example, I have String1, String2 .... Stringn. I need to convert these Strings into JsArray string.

How can I implement this?

Answer

Thomas Broyer picture Thomas Broyer · Jun 17, 2012

You don't have much choices: creating a JsArrayString and adding to it, or using JSNI.

JsArrayString arr = JavaScriptObject.createArray().cast();
arr.push(str1);
arr.push(str2);
arr.push(str3);

or

static native JsArrayString asJsArray(String str1, String str2, String str3) /*-{
  return [str1, str2, str3];
}-*/;

Obviously, the latter doesn't scale, while being faster.

It really depends what exactly you need to do.