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 == groups_chosen[j])
{*
removed = groups.splice(i , 1);*
}*
}* }[/quote] 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 == groups_chosen[j]) > * {* > * removed = groups.splice(i , 1);* > * }* > * }* > }[/quote] > 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
Could it be that you have to declare the array before you use it:
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 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*, would I? But it seems to be a “different” array…*
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 + “,”; ?? 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)
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)?