0

I'm having some difficulties with recursive functions. Can someone help me?

I have the following structure:

{
  "entity": {
    "entityLabel": "Virtual Reality",
    "parent": [
      {
        "entity": {
          "entityLabel": "Artificial Intelligence",
          "parent": [
            {
              "entity": {
                "entityLabel": "Information Technology"
              }
            }
          ]
        }
      }
    ]
  }
}

And I need the following result:

{
  "label": "Information Technology",
  "children": [
    {
      "label": "Artificial Intelligence"
      "children": [ 
        { 
          label: "Virtual Reality" 
        } 
      ]
    }
  ]
}

I couldn't accomplish the reverse order. My current code is:

const termTree = term => {
  const label = term.entity?.entityLabel
  const parentArr = term.entity?.parent
  const obj = {}

  let children = []

  if (parentArr) {
    children = parentArr.map(item => {
      return termTree(item)
    })
  }

  obj.label = label

  if (!empty(children)) obj.children = children

  return obj
}

Which results in the same order but with different labels:

{
  "label": "Virtual Reality",
  "children": [
    {
      "label": "Artificial Intelligence",
      "children": [
        {
          "label": "Information Technology"
        }
      ]
    }
  ]
}

As you can see it's reverse and it's not just a matter of changing labels.

Thanks

6
  • 4
    Whats the issue? Commented Jan 16, 2019 at 11:23
  • Properly explain your problem Commented Jan 16, 2019 at 11:24
  • That's just a task, where is your code? Commented Jan 16, 2019 at 11:24
  • I don't know how to manipulate in the reverse order. I've just posted my code. Commented Jan 16, 2019 at 12:02
  • where do you get label: 'Science' from? Commented Jan 16, 2019 at 12:17

1 Answer 1

1

You could take an iterative and recursive approach by handing over a source array for the new label and a target array for the final result with children.

By taking the above wanted format, without a children property in the most inner object, this approach take as target an object with a children. The reducing part for generating the new data structure takes only objects as return value and creates children if necessary.

var data = { entity: { entityLabel: "Virtual Reality", parent: [{ entity: { entityLabel: "Artificial Intelligence", parent: [{ entity: { entityLabel: "Information Technology" } }] } }] } },
    result = [];

[data].forEach(function iter(source, target) {
    return function ({ entity: { entityLabel, parent } }) {
        source = [entityLabel, ...source];
        if (parent) return parent.forEach(iter(source, target));
        source.reduce((t, label) => {
            var temp = (t.children = t.children || []).find(o => o.label === label);
            if (!temp) {
                t.children.push(temp = { label });
            }
            return temp;
        }, target);
    }
}([], { children: result }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.