flattening a recursive category tree

Started by Carlo Didier, November 17, 2017, 04:29:24 PM

Previous topic - Next topic

Carlo Didier

Quote from: Mario on November 17, 2017, 03:09:29 PM
If you have problems flattening a recursive category tree, let us know in the scripting board. Takes only a few lines of code.

Although I have circumvented that problem at the moment, I'd still like to know how to flatten a recursive tree.
Example result:
{
  "categories": [
    {
      "id": 8595,
      "children": [
        {
          "id": 2413,
          "children": [
            {
              "id": 2414,
              "children": []
            }
          ]
        },
        {
          "id": 2406,
          "children": [
            {
              "id": 2409,
              "children": []
            },
            {
              "id": 2408,
              "children": []
            },
            {
              "id": 2407,
              "children": []
            }, ...


would be to convert to something like:
{
  "categories": [
    {
      "id": 8595,
      "children": [...]
    },
    {
      "id": 2413,
      "children": [...]
    },
    {
      "id": 2414,
      "children": []
    },
    {
      "id": 2406,
      "children": [...]
    },
      "id": 2409,
      "children": []
    }, ...

Mario

#1
I recommend you use a forEach loop and a recursive function (a function which calls itself):



Results in



This first retrieves categories from the database (WHY and all children, in this case). Line 789.
Usually you would then process these in a recursive function and do whatever you need.

In this case, the script fills the flatcats array with a flat list of objects.
Each object consists of the id of the cat and the name. Line 795.

The local function flatten (Line 797) does the trick. It gets an array of categories as parameter.
It uses a forEach loop (798) and pushes the id and name of each category into the flatcats array.  799.

If the category has child categories (children.length > 0) it calls itself (recurses) with the children. 804.

Line 809 kicks everything of by calling flatten with the response.categories (which is the top-level of the categories returned).
Line 810 dumps the flatcats array to the browser console so you can see what it contains (2nd screen shot above).

I recommend you follow this in the debugger in your browser.
Set a breakpoint in Line 809 and step into the flatten function.

Note: You can also write categories.forEach(function(c) { but I used the more modern ES6 syntax here. I don't usually do this for IMatch Apps to keep things simpler for users who study the code.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Carlo Didier

Thanks Mario!
I could have thought myself of the recursive function. Must be at least 10 years since I needed such a thing ...
I think I'll put the function in my "toolbox" library so I can simply include it in any scripts where I need it.