Well, I have an array with objects where some elements depends of others elements.
So, I need to order it by importance (dependency of parent) to store this on a database and replace all the children's parent
property by the respective parent id
.
Example of the array:
[
{
"id": 1,
"email": "a@b.com", // unique
"parent": "c@b.com" // is nullable
},
{
"id": 2,
"email": "b@b.com",
"parent": null
},
{
"id": 3,
"email": "c@b.com",
"parent": "b@b.com"
},
{
"id": 4,
"email": "d@b.com",
"parent": "a@b.com"
},
...
]
Graphical example of dependency:
Expected result: Ordered by dependency (parent):
[
{
"id": 2,
"email": "b@b.com",
"parent": null
},
{
"id": 3,
"email": "c@b.com",
"parent": 2
},
{
"id": 1,
"email": "a@b.com",
"parent": 3
},
{
"id": 4,
"email": "d@b.com",
"parent": 1
},
...
]
To set respective parent id
I'm using (but it no ordering by parent level: parent, children, grandchildren...):
let users = [
{
"id": 1,
"email": "a@b.com", // unique
"parent": "c@b.com" // is nullable
},
{
"id": 2,
"email": "b@b.com",
"parent": null
},
{
"id": 3,
"email": "c@b.com",
"parent": "b@b.com"
},
{
"id": 4,
"email": "d@b.com",
"parent": "a@b.com"
}
];
users = users.map(user => {
user.parent = _.findIndex(users, i => user.parent === i.email);
return user;
});
P.S:
In this case, the importance
concept refers to parent
level.
So, First I need the parents, then the children, grandchildren, and so on...
I am sorry if this thread is poor in explanations, if you have doubts, I will look for the best way to express the idea.