【问题标题】:Finding parent objects in nested arrays of objects. How?在嵌套的对象数组中查找父对象。如何?
【发布时间】:2015-02-12 10:04:19
【问题描述】:

我有一个如下所示的数据结构:

var someDataStructure = [
  {
    opts: {_id:1}
  },
  {
    opts: {_id: 2},
    children: [
      {
        opts: {_id: 3},
        children: [
          {
            opts: {_id: 4}
          }
        ]
      }
    ]
  },
  {
    opts: {_id: 5}
  },
  {
    opts: {_id: 6},
    children: [
      {
        opts: {_id: 7},
        children: [
          {
            opts: {_id: 8}
          }
        ]
      }
    ]
  }  
];

这是一个对象数组,所有对象都有一个opts 属性和一个可选的children 属性。如果存在,children 属性将是同类型对象的数组。

给定任何opts._id,我需要找到所有父对象的_id。为了方便起见,我在这里给出的_id 只是按顺序排列的。 你不能假设它们是连续的整数

这个项目同时使用了 jquery 和 lodash,所以这两个库都可以使用。

所需输出示例:

  • 给定4,返回[2, 3]
  • 给定3,返回[2]
  • 给定8,返回[6, 7]
  • 给定7,返回[6]

递归并找到给定的_id 没有问题。但是,我感到很愚蠢,并且坚持维护父母的阵列。

【问题讨论】:

  • 每个opts只能有一个孩子?
  • 你确定那些期望输出的例子是正确的吗?
  • 返回 8 [6,7];给出 2 返回 []
  • @Linus 抱歉,修复了所需的输出。这就是我做太多编辑而没有足够校对的结果。
  • @AlessandroMarchisio 孩子可以嵌套到任何深度。

标签: javascript algorithm recursion data-structures


【解决方案1】:

对于一个孩子来说,这是可行的:

function writeParent(id, arr) {

    var ct = 0;
    var found = false;
    var parentsLine = []; 

    arr.some(function (e){
        parentsLine = []

        for (var curr = e; curr.children != null; curr = curr.children[0]) {
            if (id == curr.opts._id) {
                found = true;
                return true;
            } 
            parentsLine.push(curr.opts._id)    

        }  

        if (id == curr.opts._id) {
            found = true;
            return true;
        } 


    })
    if (found) {
        return parentsLine;
    } else {
        return "ERR: elm not found";
    }    
}

http://jsfiddle.net/alemarch/ufrLpLfx/11/

【讨论】:

    【解决方案2】:

    如果找到,则返回已找到状态和父级的解决方案。

    function getParentsHelper(tree, id, parents) {
        if (tree.opts._id == id) {
            return {
                found: true,
                parents: parents
            };
        }
        var result = {
            found: false,
        }
        if (tree.children) {
            $.each(tree.children, function(index, subtree) {
                var maybeParents = $.merge([], parents);
                if (tree.opts._id != undefined) {
                    maybeParents.push(tree.opts._id);
                }
                var maybeResult = getParentsHelper(subtree, id, maybeParents);
                if (maybeResult.found) {
                    result = maybeResult;
                    return false;
                }
            });
        }
        return result;
    }
    
    function getParents(data, id) {
        var tree = {
            opts: { },
            children: data
        }
        return getParentsHelper(tree, id, []);
    }
    

    使用示例:

    console.log(getParents(someDataStructure, 4).parents);
    console.log(getParents(someDataStructure, 3).parents);
    console.log(getParents(someDataStructure, 8).parents);
    console.log(getParents(someDataStructure, 7).parents);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多