1

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. If it exists the children property will be an array of the same sort of objects.

Given any opts._id, I need to find the _id of all parent objects. The _id's I give here are only sequential for convenience. 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.

Example desired output:

  • Given 4, return [2, 3].
  • Given 3, return [2].
  • Given 8, return [6, 7].
  • Given 7, return [6].

I have no problem recursing in and finding the given _id. However, I'm feeling dumb and stuck on maintaining the array of parents.

6
  • each opts can have only one children? Commented Dec 14, 2014 at 0:51
  • Are you sure those examples of desired output are correct? Commented Dec 14, 2014 at 0:53
  • Gives 8 return [6,7]; gives 2 return [] Commented Dec 14, 2014 at 1:32
  • @Linus sorry, fixed desired output. That's what I get for doing too much editing and not enough proofreading. Commented Dec 14, 2014 at 1:41
  • @AlessandroMarchisio children can be nested to any depth. Commented Dec 14, 2014 at 1:42

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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/

Comments

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.