Home > Blockchain >  Recursive function returns weird output?
Recursive function returns weird output?

Time:10-13

So i have this problem where i need to fill a list with objects of children's ids,names and parents ids and names for further backend work.

When i select a child and I recursively fill the list it gives me this output

    [
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "sss21"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "ss21"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "s1"
        },
        {
            "_id": "6328351214a6c4002121c2c5",
            "name": "new"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "s1"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "ss21"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "sss21"
        }
    ] 

I use this function to fill the list

selectionOutput(node) {
    const id = node._id;
    const name = node.name;
    if (node.parent !== null) {
      this.selection.push({_id: id, name: name});
      this.selectionOutput(node.parent);
    }
    this.selection.push({_id: id, name: name});
    return this.selection;
  }

The function expects a node with parent nodes which in this case is this one

{
            "_id": "6328351214a6c4002121c2c5",
            "name": "new"
        }

the other 3 like "s1, ss21 and sss21" are its children.

How can i make the function return just

    [
        {
            "_id": "6328351214a6c4002121c2c5",
            "name": "new"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "s1"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "ss21"
        },
        {
            "_id": "6328354914a6c4002121c2c6",
            "name": "sss21"
        }
    ] 

I should probably add that my data structure looks like this:

{
  "_id": "6328351214a6c4002121c2c5",
  "name": "new"
  "parent": null,
  "children": {
                "_id": "6328354914a6c4002121c2c6",
                "name": "s1",
                "parent": {"_id": "6328351214a6c4002121c2c5","name": "new"}
                "children": {
                              "_id": "6328354914a6c4002121c2c6",
                              "name": "ss21",
                              "parent": {"_id": "6328354914a6c4002121c2c6","name": "s1"}
                              "children":{...} Hope you get the point
        }
              },...
}

Also my function is fed with and Object that is a child and it goes up a level from there. For example child3(parent: child2) -> child2(parent:child1) -> child1(parent: parent) -> parent. I want to map all the ids all the ancestors of the initial object including the id of the initial one.

CodePudding user response:

Looking at your data structure you can try this approach:

data.reduce((accum, item) => {
      const childs = [];

      const getChilds = ({ _id, name, children}) => {
        childs.push({ _id, name});
        if (children) getChilds(children);
      };

      if (item.children) getChilds(item.children);

      return [...accum, { _id: item._id, name: item.name, children: childs }];
    }, [])

OR this one if you want all data at root level

data.reduce((accum, item) => {
      const childs = [];

      const getChilds = ({ _id, name, children}) => {
        childs.push({ _id, name});
        if (children) getChilds(children);
      };

      if (item.children) getChilds(item.children);

      return [...accum, { _id: item._id, name: item.name }, ...childs];
    }, [])
``

CodePudding user response:

One push is all that the function needed. I moved the push before the if statement and it works fine now.

selectionOutput(node) {
    const id = node._id;
    const name = node.name;
    this.selection.push({_id: id, name: name});
    if (node.parent) {
      this.selectionOutput(node.parent);
    }
    return this.selection;
  }
  • Related