I'd like to create a tree of services from a string which are properties of people. I manage to do it, but the code is quite ugly, and it's a king of practice for me for the trick is i'd like to make it with "class" of objects using "Object.create()" where the class would look like this :
let service = {
serviceFather: "",
serviceChildren: [],
people: [],
}
};
A sample of input is :
[
{
"name": "John Doe",
"service": "EE",
},
{
"name": "Jane Doe",
"service": "EE.EA",
},
{
"name": "Jack Smith",
"service": "EE.EA.EB",
},
{
"name": "Jill Smith",
"service": "EE.EA.EC"
},
{
"name": "Jake Smith",
"serviceLevel": "EE.EA.EC"
}
]
The expected output would be :
[
{
"name": "EE",
"serviceFather": "root",
"people": [
{
"name": "John Doe"
}],
"serviceChildren": [
{
"name": "EA",
"serviceFather": "EE",
"people": [
{
"name": "Jane Doe"
}],
"serviceChildren": [
{
"name": "EB",
"serviceFather": "EA",
"people": [
{
"name": "Jack Smith"
}],
"sousService": ""
},
{
"name": "EC",
"serviceFather": "EA",
"people": [
{
"name": "Jill Smith"
},
{
"name": "Jake Smith"
}],
"sousService": ""
}]
}]
}]
You could use
split
on service property and thenforEach
loop andreduce
to iterate nested tree and add to array.