Within the function fetchdataFromAPI
, I am receiving JSON data from an API response.
My goal is to identify unique regions among all child objects in the JSON and create a new filtered array of objects that match my expected output.
I have attempted to retrieve the desired output using the code below, but it only returns objects from the first set of children, not all child objects.
I would appreciate assistance in correcting my code to achieve the expected response from the function provided.
fetchdataFromAPI() {
const data = [
{
"name": "Africa",
"children": [
{
"name": "Test1",
"region": "1UL Africa"
},
{
"name": "Test2",
"region": "South Africa",
},
{
"name": "Test3",
"region": "1UL Africa"
}
]
},
{
"name": "Europe",
"children": [
{
"name": "Test4",
"region": "1UL Africa"
},
{
"name": "Test5",
"region": "Test Europe"
}
]
}
];
this.dataService.setBUSubRegionList(this.processRegion(data));
}
processRegion(buRegionList) {
const list = [];
for (const buReg of buRegionList) {
const tempBu = buReg;
if (buReg.children) {
let i = 0;
for (const buRegion of buReg.children) {
if (!buRegion.region) {
tempBu.children.splice(i, 1);
}
i++;
}
}
list.push(tempBu);
}
return list;
}
The expected output based on the provided JSON is as follows:
newData = [
{
"name": "Test1",
"region": "1UL Africa"
},
{
"name": "Test2",
"region": "South Africa",
},
{
"name": "Test5",
"region": "Test Europe"
},
];