Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT: creating associated jsarray

Tags:

arrays

gwt

I want to create a js array of type

Name(
{title : "Mr.",firstname : "Bill",lastname : "Gates"},
{title : "Mr.",firstname : "Bill",lastname : "Gates"},
{title : "Mr.",firstname : "Bill",lastname : "Gates"}
)

So basically i want to create associated array. All the examples are like converting javascript array to java but in my case i want the other way round. I will be filling this array from java.

JSArray and JsMixedArray seems to be doing this but i could figure out how to add to them.

like image 649
mvairavan Avatar asked Sep 12 '26 09:09

mvairavan


1 Answers

One approach could be to use a JSNI method to create the items/entries of your Array/Map as follows:

 JsArray arr = JavaScriptObject.createArray().cast();
 arr.push(newEntry("Mr.", "Bill", "Gates"));      

 ....

 private final native JavaScriptObject newEntry(String title, 
                               String firstname, String lastname)/*-{
     return {title: title, firstname: firstname, lastname: lastname};
 }-*/; 

You could also try to create the data structure you have in mind using the JSON utility methods: Put JSONObjects inside a JSONArray.

like image 131
mxro Avatar answered Sep 14 '26 10:09

mxro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!