0

I have original nested object which contains the huge tree kind of structure. This is is basically JSON string which is converted into JavaScript object.

Structure is like -

original = {
       type :  "table",
       children :[
           {
             type : "cell",
             children : [
                {
                   type : "label",
                   children : []
                }
             ]
           }
           {
             type : "cell",
             children : []
           }
       ]
    }

I have selected item as -

var select = original.children[1].children[0];

What I want is get the parent of selected item.

Here is sample demo - https://stackblitz.com/edit/angular-v5m9ua

Note : I need to trace over the original object to find the parent. I had looked at the other answers but they had mentioned how to design the object structure to get the parent but I don't want to change the original object.

13
  • @mirakurun and @Andy I have checked the answers which are mentioned as duplicated of but its a bit different since I don't have choice to modify the original object. Commented Dec 8, 2018 at 13:29
  • 1
    @SunilSingh — It isn't different. If you can't implement the solution, then you can't solve the problem. Commented Dec 8, 2018 at 13:39
  • 1
    Then there is no way then traversing a known ancestor object down to the one you are looking for Find an object in an array of deeply nested objects recursively, javascript find by value deep in a nested object/array but based on your question you already know that, so what is the problem you have with that approach? Commented Dec 8, 2018 at 13:47
  • 1
    @Quentin - this is really weird. You are forcing someone to implemented the solution just because it looks right to you and its top voted. You cannot, at least let someone help on this. I cannot create my problem state from solution. It must be vice versa. Commented Dec 8, 2018 at 13:51
  • 1
    Javascript objects: get parent Commented Dec 8, 2018 at 13:56

2 Answers 2

1

You could create recursive function with for...in loop and return last parent element that was of object type.

const data = {
  type: "table",
  children: [{
    type: "cell",
    children: [{
      type: "label",
      children: []
    }]
  }, {
    type: "cell",
    children: []
  }]
}
var select = data.children[0].children[0];

function getParent(data, obj, parent = null) {
  let result = null;

  (function loop(data, obj, parent) {
    if (typeof data == 'object' && !Array.isArray(data)) {
      parent = data
    }

    for (let i in data) {
      if (select == data[i]) {
        result = parent;
        break;
      }

      if (typeof data[i] == 'object') {
        loop(data[i], obj, parent)
      }
    }
  })(data, obj, parent)

  return result;
}

let parent = getParent(data, select)
console.log(parent)

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

1 Comment

Your implementation can handle the more complicated unknown tree structure including my case. However answer of @Jonas Wilms is pretty simple which meets my requirement. Thank you for your answer :)
0

You could search the tree:

 findParent(root, toFind) {
    for(const child of root.children || []) {
      if(child === toFind){
         return root;
      } 
      const result = this.findParent(child, toFind);
      if(result){
        return result;
      } 
    }
}

That can be used as:

  findParent(original, select)

6 Comments

You could have added a new answer to the duplicate instead of reopening this quesiton.
@quentin again. the questions are different. Thats why I reopened.
Its working with selected item this.parent.children[0] but not with ` this.parent.children[1];` - here is the demo - stackblitz.com/edit/angular-v5m9ua
@sunil cause the structure is different. All nodes have to have a children property, or you habe to handle the case that root.children is undefined.
I edited the answer which is working for me. It require more testing but looks fine so far. Thank you for your help :)
|

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.