【问题标题】:How can I acces to object I know to key dynamically like tree我怎样才能访问我知道动态键像树一样的对象
【发布时间】:2021-02-10 03:10:20
【问题描述】:

我有像下面这样的动态数据树结构,但数据是可变的(例如可以将子元素添加到 id:6)元素,我想访问它我知道 id 名称节点例如 id 是 7 我这样访问它

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

但是如果我只知道 id 名称,如果我不知道它在哪个节点中,我怎么能从给定的树结构中找到它 为了更好地理解例如我只知道 id 是 7 我想 tree.children[0].children[0].children[1] 这个路径我怎么能动态地做

  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"
      }
    ]
  };

【问题讨论】:

  • 这次请不要删除您的问题。我正在研究解决方案,但无法提交,因为您删除了之前的问题:)
  • 这能回答你的问题吗? Find by key deep in a nested array

标签: javascript reactjs algorithm sorting tree


【解决方案1】:

要访问它,请使用document.getElementById("tree7", "tree 6")。然后只需添加一些执行内容。或者使用变量

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

var treesomething = tree6 + tree7;

【讨论】:

  • 问题并没有说这棵树是 DOM 元素的树。这个解决方案做了一个我认为不准确的假设。
【解决方案2】:

类似这样的:

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'));

【讨论】:

  • 我想访问 id 的 7 元素路径,例如 tree.children[0].children[0].children[1]
  • 是的,路径是正确的,但是让 path = findById(7, tree, 'tree') 然后 path[2] 是字符串不给这个 {id: 7, name: "tree7"},我该怎么做
  • 我不确定我理解你的意思。如果你做const [obj, path] = findById(7, tree, 'tree');,那么你就有了对象和字符串路径。如果你想使用路径来获取项目,你可以eval(path),它会得到它。
【解决方案3】:

这就是你要找的吗?

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);

【讨论】:

  • 我想访问 id 的 7 元素路径,例如 tree.children[0].children[0].children[1]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多