I want to create a function that gets the "parent" in a recursive array loop, and make an output array of those "used" parents.
It's a bit hard to explain, but take a look at the example:
const regions = [{
name: 'Europe',
subRegions: [{
name: 'BeNeLux',
territories: [{
code: 'NL',
name: 'Netherlands'
}, {
code: 'DE',
name: 'Germany'
}, {
code: 'LU',
name: 'Luxembourg'
}]
}],
territories: [{
code: 'UK',
name: 'United Kingdom'
}, {
code: 'AL',
name: 'Albania'
}, {
code: 'ZW',
name: 'Switzerland'
}]
}, {
name: 'Africa',
territories: [{
code: 'GH',
name: 'Ghana'
}]
}]
const selectedTerritories = ['NL', 'UK', 'GH']
At this point I need a function, that searches for all the TOP regions of a territory by code, so the output would look like this:
const activeRegions = ['Europe', 'Africa']
A thing to note is that, there is a subRegion within Europe (BeNeLux), and the recursion gets to that point, it should NOT return BeNeLux as an active region but Europe instead.
This is what I have tried, but has duplicate subregion names and it ignores the "parent" recursion discover requirement:
const getRegionsLabelFromTerritoryList = (activeTerritories, regions, activeRegions = []) => {
regions.forEach((region) => {
if (region.territories) {
region.territories.forEach(t => {
if (activeTerritories.includes(t.code)) {
activeRegions.push(region)
}
})
}
if (region.subRegions) {
getRegionsLabelFromTerritoryList(region.subRegions, activeRegions)
}
})
return activeRegions
}
subRegionofregionsregionsarray contains one object (Europe), and one array (an array which contains Africa), which seems quite unusual. Should the Africa object be on the top level of the outer array instead?