Converting an array into a string

Is there a function available to convert an array into a string? I need to convert [1, 2, 3] into ‘1, 2, 3’ for use in a SQL query.

myArray.toString() doesn’t work. Equally, the functions utils.stringLeft(), utils.stringRight() can’t be used to edit out the brackets.

Morley, since the last Servoy 2.1 we have a tree node ‘Array’. Here you will find the function .join(delimiter)…

Also googling for ‘javascript+array’ brought me here: http://www.w3schools.com/js/js_obj_array.asp

Morley,

try this and ask somone else why:

var comArray = databaseManager.getFoundSetDataProviderAsArray(foundset,'company_id');
var newArray = new Array();
for ( var i = 0 ; i < comArray.length ; i++ )
{
	newArray[i] = comArray[i];
}
var clientIDsToQuery = newArray.toString();

Patrick

As Marcel already pointed out you can do this with the join function.

var myArray =['this','is','how','this','function','works'];
var myString = myArray.join(' ');

// Show the result
application.output(myString);

→ this is how this function works

Thus to convert [1, 2, 3] into ‘1, 2, 3’ you do:

var myArray =[1,2,3];
var myString = myArray.join(', ');

// Show the result
application.output(myString);

→ 1, 2, 3

No need for looping.

It remains an interesting question why myArray.toString() does not lead to the same result.

I have had a similar problem once and figured out, that the resulting array is a javaarray and not a Javasript-Array. With a javaarray some Array functions just don’t work as expected in JavaScript, like splice. But obviously, getFoundSetDataProviderAsArray doesn’t return a Javascript Array.

The first question is.. Why do you need to convert a array to that string of a sql query??

That shouldn’t be done anyway because i think you want to do this:

String sql = “select x from y where z in (” + array.join(‘,’) + “)”;
databasemanager.getDatasetByQuery(sql);

yes?

you should do:

String sql = “select x from y where z in (?,?,?)”;
databasemanager.getDatasetByQuery(sql,array);
and then have the same number of ? in as the size of the array.

jcompagner:
String sql = “select x from y where z in (” + array.join(‘,’) + “)”;
databasemanager.getDatasetByQuery(sql);

You should do:

String sql = “select x from y where z in (?,?,?)”;
databasemanager.getDatasetByQuery(sql,array);
and then have the same number of ? in as the size of the array.

Could you clarify why the latter is preferable to the former? Note that the number of elements in the array will be unknown.

You can always get the size of an array with the length property;

myArray.length

the database can preparse youre sql and cache it ect.
Also string don’t have to be qouted by you but this is all handled by the driver.

Just always try to use sql with ? instead of direct values.

Johan, your syntax:

String sql = "select x from y where z in (?,?,?)";
databasemanager.getDatasetByQuery(sql,array);

produces JS errors.

I’ve also tried:

databaseManager.getDataSetByQuery(controller.getServerName(), sql, comArray, 10);

databaseManager.getDataSetByQuery(controller.getServerName(), sql(comArray), null, 10);

All three produce errors.

see http://forum.servoy.com/viewtopic.php?t=3505

What size does the array have in youre example???

look what i said above:

“and then have the same number of ? in as the size of the array.”

Trying to get my mind around working with arrays in Servoy.

I have a method that finds the groups a user is a member of and stores them in an array…I store the array as string into a global so I can use it later in a different method.

method #1
var arrayGroups = new Array();
var arrayGroups = databaseManager.getFoundSetDataProviderAsArray(foundset,‘group_id’);
globals.gGroupString = arrayGroups.join(', ');

Then in another method I want to put the array as string stored in the global above into a text field:

method#2
forms._menu.controller.setDataProviderValue(‘groups_array’,globals.gGroupString);

I would expect ‘groups_array’ to display something like ‘45,32, 12’ but instead I get this:
java.lang.Object@a27a33/n java.lang.Object@a27a33/n java.lang.Object@a27a33/n java.lang.Object@a27a33/n java.lang.Object@a27a33

I am obviously misunderstanding something here. Thanks for any help on this.

if i do this:

var arrayGroups = databaseManager.getFoundSetDataProviderAsArray(foundset,'some_data');
var str = arrayGroups.join(', '); 
application.output(str)

then it outputs exactly what it should.
So something else should go wrong

Why are you setting that global in a dataprovider?
Why not just build a textfield on that global?

jcompagner:
Why are you setting that global in a dataprovider?
Why not just build a textfield on that global?

Sorry…not sure of the distinction between dataprovider and textfield.

Arrays returned by database manager functions are different than arrays that you create yourself. This is distinctly highlighted when you view the array variables in the debugger when running the following code:

//this returns "[7,8]"
var array = databaseManager.getFoundSetDataProviderAsArray(foundset,'id_main');

//this returns "7,8"
var x = new Array('7','8');

So the extra brackets create a problem. Convert the “bracketed” array to a “javascript” array and then your array will behave as you expect. Here is a snipet of code where I first return the “bracketed” array then loop through it and load a “javascript” array:

//get fm field names
var server = controller.getServerName();
var maxRows = forms.lvl1_bridge_detail.bridge_to_fmfield.getSize();
var FMFieldsQuery = 'select f_name from fmsql_fmfield where id_bridge = ' + globals.select_bridge + 'order by f_order;';
var FMFieldsDataset = databaseManager.getDataSetByQuery(server, FMFieldsQuery, null, maxRows);
var tempARY = FMFieldsDataset.getColumnAsArray(1);
var FMFieldsARY = new Array;
for (var a in tempARY)
{
	FMFieldsARY[a] = tempARY[a];
}

Would be nice to get these two array forms standardized.

what do you mean this returns “xxx” ?
How does it return what code do you use to convert that array back into a string? The split function?
Please give a complete example so that i can see if i can make it the same

My first code snippet is complete. Run it with the debugger on and you will see that the values of the two arrays are displayed differently. The first one has brackets around the values and the second one doesn’t.

In my situation I’m not using the array in a sql query so not sure if there is a problem with converting to a string. My problem comes from passing arrays to a custom plugin. My plugin doesn’t accept the “bracketed” array form so I first have to convert it to a “javascript” array before sending it to my plugin.

My plugin is defined to accept that value as String variable. I bring this up in this thread to highlight that there are two forms of arrays in the Servoy method editor. Not sure if they behave differently with other functions (such as the standard array functions or loading a value list, etc). I would have to test further. But as usual, I fixed my problem and moved on :)

ok. i think i know why it doesn’t work
That is because the first is a Object not a String so if you made youre plugin so that it supports Object then it would work.

I think the native javascript array is just trying to convert to the right type for you when calling. I will try to fix it that als a native java object tries to do that.

in RC4 it should work fine now. There shouldn’t be any difference between the 2 ‘types’ of arrays.