java script question on arrays

Questions, tips and tricks and techniques for scripting in Servoy

java script question on arrays

Postby patrick » Fri Feb 13, 2004 6:17 pm

Hello,

I have spent some hours now on a little problem that I now solved but don't really understand.

I want to get an array of all usergroups (security) that a user is NOT already assigned to.

Here is what I did first:

var groups = security.getGroups().getColumnAsArray(2);
var groups_chosen = security.getUserGroups(uid_user_servoy).getColumnAsArray(2);
var removed;

for ( var i = 0 ; i < groups.length ; i++ )
{
for ( var j = 0 ; j < groups_chosen.length ; j++ )
{
if (groups[i] == groups_chosen[j])
{
removed = groups.splice(i , 1);
}
}
}


In my example, there is 3 groups (Administrators, Users, Test). The user I am testing with is in Administrators and Users. The result should be "Test". But with the above code, the result is "Test, Users, Users".

Now I do this:

var thegroups = security.getGroups().getColumnAsArray(2);
var groups = new Array();
for (k = 0; k < thegroups.length; k++)
{
groups[k] = thegroups[k]
}

var thegroups_chosen = security.getUserGroups(uid_user_servoy).getColumnAsArray(2);
var groups_chosen = new Array();
for (l = 0; l < thegroups_chosen.length; l++)
{
groups_chosen[l] = thegroups_chosen[l]
}

var removed;

for ( var i = 0 ; i < groups.length ; i++ )
{
for ( var j = 0 ; j < groups_chosen.length ; j++ )
{
if (groups[i] == groups_chosen[j])
{
removed = groups.splice(i , 1);
}
}
}


And that works as intended.

Can someone explain to me the difference between the Array "groups" from the upper code and the Array "groups" from the lower code? Or does getColumnAsArray deliver "some other type of Array"?

Thanks for your time.

Patrick
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby WiderGates » Fri Feb 13, 2004 11:44 pm

Could it be that you have to declare the array before you use it:

Code: Select all
var groups = new Array();
var groups_chosen = new Array();
groups = security.getGroups().getColumnAsArray();
groups_chosen = security.getUserGroups(uid_user_servoy).getColumnAsArray();
var removed;

for ( var i = 0 ; i < groups.length ; i++ )
{
for ( var j = 0 ; j < groups_chosen.length ; j++ )
{
if (groups[i] == groups_chosen[j])
{
removed = groups.splice(i , 1);
}
}
}


I havenM-4t tested that, just an idea.
Version R2 2.0 rc8-build 267; Java version 1.4.2_03-b02 (Windows 2000)

Dieter Jekal
WiderGates
WiderGates
 
Posts: 37
Joined: Thu Aug 28, 2003 9:46 am
Location: Schnaittenbach, Germany

Postby patrick » Sat Feb 14, 2004 10:37 am

I will try that. But for sure "groups" is an array already. Otherwise I wouldn't be able to populate my own array "thegroups" through groups[i], would I? But it seems to be a "different" array...
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby jcompagner » Sat Feb 14, 2004 3:50 pm

first why are you doing this:

removed = groups.splice(i , 1)

why use splice because splice will remove the i position from the array.
and that will cause a problem in youre loop because i+1 value will now be on the index i but then you increment i so the value that was on i+1 before the splice will never be tested!

why don't you do:

removed += groups[i] + ",";

??

but you did find a problem
if you have a real javascript array then splice works like it should, but with a javaarray (the thing you get back from getColumnAsArrya(2)) it behavious a bit different... (it doesn't shrink)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby patrick » Sat Feb 14, 2004 5:39 pm

Hello Johan,

thanks for your reply. I am using splice because I exactly want the resulting Array to be "shorter" than the original. I want those entries that are in the other array to be removed. I don't need the variable "removed", I am just using it to see what gets removed in the debugger.

I am not sure either that I am missing a test. Yes, the array gets shorter but I still hit all the values, since at least the shorter length is walked through. My other method returns the wanted result, so I think all tests are passed.

About your remark about the array returned: it looks like the javaarray doesn't fully comply to java script methods? So the way I tried is the way to do it (make a real java script array from the javaarray)?

Thanks
Patrick
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby jcompagner » Sun Feb 15, 2004 12:45 am

yes you are missing a test with that method.

i give you an example:

[patrick,johan,jan] are the three things in the array.

now the index = 0; (so it is on patrick)
then you do splice, then the array is this:

[johan,jan]

then you increment the index to 1

so you are pointing to jan now and not to johan..

just run this code:

var tmp = new Array("patrick","jan","johan");
for(var i =0; i<tmp.length;i++)
{
tmp.splice(i,1);
}

and you will see that jan is still in the array..
such kind of loops has to be loop from the top..

var tmp = new Array("patrick","jan","johan");
for(var i =tmp.length; i-->0;)
{
tmp.splice(i,1);
}

then it goes well.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Methods

Who is online

Users browsing this forum: No registered users and 40 guests

cron