1

I have a bunch of tasks and I need to find all children based on the task I click. I would like an array of position [ 1,2, 3] to come back so I can work with those rows.

Here is an example.

    var taskAR = [
    {
        "isParent": true,
        "parentID": null,
        "level": 0
    },
    {
        "isParent": true,
        "parentID": 0,
        "level": 1
    },
    {
        "isParent": true,
        "parentID": 1,
        "level": 2
    },
    {
        "isParent": false,
        "parentID": 2,
        "level": 3
    },
    {
        "isParent": false,
        "parentID": 2,
        "level": 3
    },
    {
        "isParent": false,
        "parentID": null,
        "level": 0
    },
    {
        "isParent": true,
        "parentID": null,
        "level": 0
    },
    {
        "isParent": false,
        "parentID": 7,
        "level": 1
    }

];


function getNestedChildren(arr, parentID) {
    var out = []
    for (var x = 0, len = arr.length; x < len ; x++) {
        var d = arr[x];

        if (d.parentID == parentID) {
            var children = getNestedChildren(arr, x);

            if (children.length) {
                d.parentID = x;
            }
            out.push(x)
        }
    }
    return out
    }

   getNestedChildren(this.master.tasks, parentID)

thanks for the help

5
  • 4
    What is the actual problem? Commented Feb 20, 2015 at 15:36
  • And, do you have the ability to change the structure of your data? There are MUCH better ways to represent parent-cheild relationships than what you are using/ Commented Feb 20, 2015 at 15:38
  • Yep I can change it if there is a better way. The idea is to find all the children of the parent and then work with them both from the found array and on the display. Commented Feb 20, 2015 at 15:50
  • the problem I think is here: if (children.length) { d.parentID = x; } do I need to put another loop in there to get the position?? Commented Feb 20, 2015 at 15:52
  • BTW, a nice way to organize parent-child relations can be seen in DOM: a binary tree with left branch pointing to a firstChild and right branch pointing to a nextSibling. It might be augumented with the idea of skip list to eliminate the need of flattening... Commented Feb 20, 2015 at 16:21

1 Answer 1

1

Two problems with this code: First is that you are not accumulating values. You need the output from the nested call to getNestedChildren to be returned in your out array. Second, I don't know why you are changing any parentIDs when this runs, it should be a read-only operation.

Take out

        if (children.length) {
            d.parentID = x;
        }

and replace it with

        out = out.concat(children);
Sign up to request clarification or add additional context in comments.

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.