I have incoming data in this format:
const worldMap = [
{
"name": "Germany",
"parentId": null,
"type": "Country",
"value": "country:unique:key:1234",
"id": "1",
},
{
"name": "North Rhine",
"parentId": "1",
"type": "State",
"value": "state:unique:key:1234",
"id": "2",
},
{
"name": "Berlin",
"parentId": "1",
"type": "State",
"value": "state:unique:key:1234",
"id": "3",
},
{
"name": "Dusseldorf",
"parentId": "2",
"type": "city",
"value": "city:unique:key:1234",
"id": "4",
},
{
"name": "India",
"parentId": null,
"type": "Country",
"value": "country:unique:key:1234",
"id": "5",
},
];
I want the output to be something like this:
[
{
label: "Germany",
value: "country:unique:key:1234",
subs: [
{
label: "North Rhine",
value: "state:unique:key:1234",
subs: [
{
label: "Dusseldorf",
value: "city:unique:key:1234",
}
]
},
{
label: "Berlin",
value: "state:unique:key:1234",
}
]
}
,
{
"label": "India",
"value": "country:unique:key:1234"
}
]
Basically, it is a three dimensional array with first level being the Countrie, second States and third Cities. I have tried the following code:
let tempCountries = [];
worldMap.map((world) => {
if (world.parentId == null && world.type == "Country") {
tempCountries.push({label: world.name, value: world.value, id: world.id});
}
});
console.log("=== countries ===", tempCountries);
tempCountries.map((tempCountry) => {
const states = worldMap.find((x) => x.parentId == tempCountry.id);
console.log("=== states ===", states);
if (states !== undefined) {
}
});
In the first loop I got all the values for the countries. Next I am trying to append states and cities to the original countries, I got from the first loop. But I am not able to do so. The code should be verbose with minimum number of loops. Could anyone please help achieve this ?
Thanks