-1

I have dynamic data tree structure like below but data is changeable (for example could be added children to id: 6) element, I want to access it I know id name node for example id is 7 I access it like that

tree.children[0].children[0].children[1]

but if i only knew id name and how could I find it from the given tree structure if I didn't know what node it was in for better understanding for example Im just know the id is 7 I want to tree.children[0].children[0].children[1] this path how can I do dynamically

  let tree = {
    id: 1,
    name: "tree1",
    children: [
      {
        id: 2,
        name: "tree2",
        children: [
          {
            id: 4,
            name: "tree4",
            children: [
              {
                id: 6,
                name: "tree6"
              },
              {
                id: 7,
                name: "tree7"
              }
            ]
          },
          {
            id: 5,
            name: "tree5"
          }
        ]
      },
      {
        id: 3,
        name: "tree3"
      }
    ]
  };
Alisan26
  • 323
  • 2
  • 12
  • Please do not delete your question this time. I was working on a solution and could not submit it because you deleted your previous question :) – IvanD Oct 27 '20 at 19:08
  • 1
    Does this answer your question? [Find by key deep in a nested array](https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-array) – Emile Bergeron Oct 27 '20 at 19:34

3 Answers3

3

Something like this:

let tree = {
    id: 1,
    name: "tree1",
    children: [
      {
        id: 2,
        name: "tree2",
        children: [
          {
            id: 4,
            name: "tree4",
            children: [
              {
                id: 6,
                name: "tree6"
              },
              {
                id: 7,
                name: "tree7"
              }
            ]
          },
          {
            id: 5,
            name: "tree5"
          }
        ]
      },
      {
        id: 3,
        name: "tree3"
      }
    ]
  };
  
const findById = (id, obj, path = 'tree') => {
    if (obj.id === id) return [obj, path];
    else if (obj.children) {
        for (let i = 0; i < obj.children.length; i++) {
            const [child, p] = findById(id, obj.children[i], path + `.children[${i}]`);
            if (child) {
                return [child, p];
            }
        }
    }
    return [null, path];
}

console.log(findById(7, tree, 'tree'));
dave
  • 62,300
  • 5
  • 72
  • 93
  • I want acces to id's 7 element path like tree.children[0].children[0].children[1] – Alisan26 Oct 27 '20 at 19:15
  • Yes is path is correct but let path = findById(7, tree, 'tree') then path[2] is string not giving to this {id: 7, name: "tree7"}, how can I do that – Alisan26 Oct 27 '20 at 19:40
  • I'm not sure I understand what you mean. If you do `const [obj, path] = findById(7, tree, 'tree');`, then you have the object and the string path. If you want to use the path to get to the item, you could do `eval(path)` and it will get it. – dave Oct 27 '20 at 19:45
0

Is this what you're looking for?

let tree = {
  id: 1,
  name: "tree1",
  children: [{
      id: 2,
      name: "tree2",
      children: [{
          id: 4,
          name: "tree4",
          children: [{
              id: 6,
              name: "tree6"
            },
            {
              id: 7,
              name: "tree7"
            }
          ]
        },
        {
          id: 5,
          name: "tree5"
        }
      ]
    },
    {
      id: 3,
      name: "tree3"
    }
  ]
};

const findNodeById = (node, id) => {
  if (node.id === id) {
    return node
  } else if (node.children && node.children.length > 0) {
    for (let child of node.children) {
      node = findNodeById(child, id);
      if (node)
        return node
    }
  } else {
    return null;
  }
}

const node = findNodeById(tree, 7);
console.log(node);
Mário Garcia
  • 553
  • 3
  • 13
-2

To access it use document.getElementById("tree7", "tree 6"). Then just add some execssive content. Or use a variable

var tree6 = document.getElementById("tree6");
var tree7 = document.getElementById("tree7");

var treesomething = tree6 + tree7;
derloopkat
  • 6,232
  • 16
  • 38
  • 45
Shed
  • 7
  • 4
  • 2
    The question doesn't say this tree is a tree of DOM elements. This solution makes an assumption I don't think is accurate. – Brian Thompson Oct 27 '20 at 19:08