简体   繁体   English

在嵌套的对象数组中查找父对象。 怎么样?

[英]Finding parent objects in nested arrays of objects. How?

I have a data structure that looks like this: 我有一个如下所示的数据结构:

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

That's an array of objects, all with an opts property, and an optional children property. 这是一个对象数组,都有opts属性,还有一个可选的children属性。 If it exists the children property will be an array of the same sort of objects. 如果存在,则children属性将是相同类型对象的数组。

Given any opts._id , I need to find the _id of all parent objects. 给定任何opts._id ,我需要找到所有父对象的_id The _id 's I give here are only sequential for convenience. 为了方便,我在这里给出的_id只是顺序的。 You may not assume they are sequential integers 您可能不认为它们是连续的整数

This project is using both jquery and lodash so both of those libraries are available for use. 该项目同时使用jquery和lodash,因此这两个库都可以使用。

Example desired output: 示例所需输出:

  • Given 4 , return [2, 3] . 给定4 ,返回[2, 3]
  • Given 3 , return [2] . 给出3 ,返回[2]
  • Given 8 , return [6, 7] . 给定8 ,返回[6, 7]
  • Given 7 , return [6] . 给出7 ,返回[6]

I have no problem recursing in and finding the given _id . 我没有问题递归并找到给定的_id However, I'm feeling dumb and stuck on maintaining the array of parents. 然而,我感到愚蠢,坚持维持父母的阵容。

A solution returning found status and parents if found. 如果找到则返回找到状态和父母的解决方案。

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, []);
}

Usage example: 用法示例:

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

for one children this works: 这对一个孩子有效:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM