Author Topic: Help with Recursive loop through Category Children  (Read 161 times)

Jingo

  • Super Hero
  • ****
  • Posts: 1541
Help with Recursive loop through Category Children
« on: February 23, 2020, 10:08:39 PM »
Hi Mario - I am 90% done with another app that will automatically delete "empty" @Keyword categories.  The app currently will locate and display all empty categories in @Keywords.. but only for a single ".children" level... because users can have a large number of empty nested categories - I need an elegant way to loop through them regardless if there are 1 or 100 levels.  I tried to come up with a recursive function that would do the job.. but couldn't fully get it to work.  Do you have some JS code that will do this?  I need something similar to:

Code: [Select]

Function RecurseChildren(response-array) {

if (response-array[id].children[id2].ChildrenCount > 0) {
  next-level = response-arryay[id].children[id2].children;
  RecurseChildren(next-level)}

  else {if (response-array[id].children[id2].filesCount == 0) {
                    delete-array = response-array[id].children[id2].name;
                      }}
}


Thanks!

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 25472
Re: Help with Recursive loop through Category Children
« Reply #1 on: February 23, 2020, 10:32:42 PM »
Do not directly manipulate data-driven categories from apps that way. Not recommended.

No app needed anyway. Just disable the option to keep empty @Keyword categories.
This is an option because keeping empty @Keywords categories allows users to reuse them for assignment later. If you don't want this, just reset the corresponding option for @Keywords.

See the help: Keep Empty Categories

Jingo

  • Super Hero
  • ****
  • Posts: 1541
Re: Help with Recursive loop through Category Children
« Reply #2 on: February 23, 2020, 11:10:42 PM »
Do not directly manipulate data-driven categories from apps that way. Not recommended.

No app needed anyway. Just disable the option to keep empty @Keyword categories.
This is an option because keeping empty @Keywords categories allows users to reuse them for assignment later. If you don't want this, just reset the corresponding option for @Keywords.

See the help: Keep Empty Categories

I have no idea how I missed that option... I poured through the help!  Thx...  thought an app would be ok because you are able to manually delete the data-driven categories and I'd be calling the standard delete category endpoint.

PS: I'd be curious how this could be done anyway if you have some time/thoughts on the code.. ie: how to recurse through the entire "children" sub-array to say gather all the names.  A  For-Each only takes you through one level... so how to loop/recourse through infinite levels?

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 25472
Re: Help with Recursive loop through Category Children
« Reply #3 on: February 23, 2020, 11:54:54 PM »
I'm currently preparing IMatch 2020 and the compatibility update for IMatch Anywhere. I have unfortunately no time to debug user scripts.
A simple recursion would do. Mind the asynchronous nature of endpoints or use async/await to simply your code.

Jingo

  • Super Hero
  • ****
  • Posts: 1541
Re: Help with Recursive loop through Category Children
« Reply #4 on: February 24, 2020, 01:01:21 PM »
I'm currently preparing IMatch 2020 and the compatibility update for IMatch Anywhere. I have unfortunately no time to debug user scripts.
A simple recursion would do. Mind the asynchronous nature of endpoints or use async/await to simply your code.

No worries - I know you are busy... I'll table this for another time when I too have some time to play... thx.