Objectlist.length fails after deleting objects from the list

Translate this pagebookmark or share this page
Martin's picture

Posted: 2012-03-21
1076 views | 5 comments | category: ARIS Community

0
show all articles

 

Hi Community,

I am quite new to scripting in ARIS BA and struggle with the conversion of old VBA scripts. One topic that causes frequent issues is determing the length of an object list (I guess it is called ABOSubjectList) after deleting some of the objects. E.g. the script creates a list of object occurences like this:

oobjocclist.value = omodel.ObjOccListFilter(Constants.OT_FUNC)

But then I need to delete some of the occurences from the list e.g. because they are inactive, have the wrong symbol or whatever. The converted script does it usually by the delete function defined in the convertertools.js . It looks like this with "k" being just the index of the object that is being deleted:

oobjocclist= __delete(oobjocclist, k)

This works quite well but afterwards I am not able to find out the length of the object list. Usually I am using:

oobjocclist.value.length

but after having deleted some elements it causes an error like "TypeError: Cannot read property "length" from undefined". In most cases I have found a workaround like reading the length into a variable upfront but in some cases this is not possible/ bothersome. Any ideas how to avoid this error? Would it be better to transform the object list into an array first and then work with the array (not sure I am able to do this either)?

Any suggestions or sample code is greatly appreciated.

Cheers,

Martin

Comments
Vladimir's picture

Hi Martin,

why do you use value property?

ObjOccListFilter

get you array of functions, so you can use 

oobjocclist.length

only.

Martin's picture

Hi Vladimir,

Yes, I was wondering about this as well. I think this was done automatically by converting the VBA script into Java. But sometime I have it with value, sometimes without. I'll try it without .value.

Thanks,

Martin

Martin's picture

Ah - I think I got it. Somehow this "__delete" function from the convertertool is creating a mess. If I have understood it correctly it just moves the object that shall be deleted to the end of the array and then reduces the length of the array by one. Like the following with "arr" being the objectlist and "obj" the object to be deleted: 

if(arr[i]==obj) {
        for(j=i+1;j<arr.length;j++) {
          arr[j-1] = arr[j];
        }
        arr.length = arr.length - 1;

Anyway - I am going to avoid this function. Instead "splice" just works perfectly. In my case:

oobjocclist.splice(k,1);

It removes one (1) object from the array at the position of k - so easy!

And Vladimir, you have been right. The value property was nonsense. This pointed me in the right direct to see what other methods are available for arrays. Thanks for the hint!

Best regards, Martin

Vladimir's picture

Hi Martin,

one more thing - try to delete occurences by Occ.Remove() method and I recommend you to decrement variable in for loop:

for (i = oobjocclist.length - 1; i >= 0; i--)
     Occ.Remove();
Martin's picture

Thanks Vladimir, good point. But in this case I want to keep the occurence in the model, I just want to delete the object from the array as it is not needed for further operations in the script.

Martin