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.