gpt4 book ai didi

javascript - 来自对象数组的递归数组父匹配

转载 作者:行者123 更新时间:2023-11-30 11:02:02 25 4
gpt4 key购买 nike

我想创建一个函数,在递归数组循环中获取“父级”,并生成那些“已用”父级的输出数组。

解释起来有点费劲,但是看例子:

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']

此时我需要一个函数,通过代码搜索领土的所有 TOP 区域,因此输出将如下所示:

const activeRegions = ['Europe', 'Africa']

需要注意的是,Europe (BeNeLux) 中有一个子区域,递归到那个点,它不应该返回 BeNeLux 作为事件区域,但 Europe 而不是。

这是我尝试过的方法,但是有重复的子区域名称并且它忽略了“父”递归发现要求:

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
}

最佳答案

假设 Africa 对象应该在顶层,根据它们的 subRegions 是否满足 filter 顶层对象递归测试(检查 selectedTerritories 是否包含被迭代对象的代码,或者是否有任何 subRegions 子项通过测试的测试):

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'];

const regionPasses = ({ subRegions, territories }) => (
territories.some(({ code }) => selectedTerritories.includes(code))
|| (subRegions && subRegions.some(regionPasses))
);

const topSelected = regions
.filter(regionPasses)
.map(({ name }) => name);
console.log(topSelected);

为了降低计算复杂度,您可以先将 selectedTerritories 转换为 Set(将 O(n) 操作转换为 O(1) 操作):

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 = new Set(['NL', 'UK', 'GH']);

const regionPasses = ({ subRegions, territories }) => (
territories.some(({ code }) => selectedTerritories.has(code))
|| (subRegions && subRegions.some(regionPasses))
);

const topSelected = regions
.filter(regionPasses)
.map(({ name }) => name);
console.log(topSelected);

您可以使用单个外部循环而不是两个(.reduce 或类似的东西而不是 .filter 后跟 .map 来获得相同的结果>),但我认为这更清楚。

关于javascript - 来自对象数组的递归数组父匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57342548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com