gpt4 book ai didi

Javascript复杂数组过滤器

转载 作者:行者123 更新时间:2023-12-01 00:23:39 28 4
gpt4 key购买 nike

我有这个对象数组,它们都有嵌套数组,每个元素都有另一个数组,每个元素都有......你明白了。深度嵌套数组。

让我通过一个例子来解释我想要实现的目标。我有一份团体名单。每个组都有一个英雄列表,每个英雄都有一个能力列表,每个能力都有一个优势列表。

我希望能够使用 mat-stepper 迭代每个步骤,选择组,然后选择英雄,然后选择力量,最后选择强度。因此,当从垫选中选择一个组时,下一步垫选将仅保留属于该组的英雄,依此类推,直到强度为止。

此组列表的示例可能如下所示:

[
{
"id": "g1",
"group": "1",
"heroes": [
{
"id": "g1h1",
"hero": "hero1",
"powers": [
{
"id": "g1h1p1",
"power": "pyromancy",
"strengths": [
{
"id": "g1h1p1s1",
"strength": "undead"
}
]
},
{
"id": "g1h1p2",
"power": "light",
"strengths": [
{
"id": "g1h1p2s1",
"strength": "shadow people"
},
{
"id": "g1h1p2s2",
"strength": "guidence"
}
]
}
]
},
{
"id": "g1h2",
"hero": "hero2",
"powers": []
}
]
},
{
"id": "g2",
"group": "2",
"heroes": [
{
"id": "g2h1",
"hero": "hero1",
"powers": [
{
"id": "g2h1p1",
"power": "electromancy",
"strengths": [
{
"id": "g2h1p1s1",
"strength": "metal armor"
},
{
"id": "g2h1p1s2",
"strength": "metal shields"
}
]
},
{
"id": "g2h1p2",
"power": "invisibility",
"strengths": []
}
]
}
]
},
{
"id": "g3",
"group": "group3",
"heroes": []
}
]

列表中的每个元素都有自己唯一的 ID。好消息是我已经实现并工作了。不过,我希望能够根据已选择的所有内容以及仅那些具有优势的能力来过滤选择。

所以移除要求如下:

  • 如果优势已全部添加到所选优势列表中,则将其作为可能的选择删除
  • 如果在任何时候某个元素有一个空列表,则将其作为可能的选择删除。因此,如果某个力量没有优势或者英雄没有力量,那么就将其移除。

因此,使用上面的示例,如果我选择的列表已准备好包含不死生物影子人金属盾牌(id值,而不是实际值) text)那么我期待返回的过滤列表应该是这样的:

[
{
"id": "g1",
"group": "1",
"heroes": [
{
"id": "g1h1",
"hero": "hero1",
"powers": [
{
"id": "g1h1p1",
"power": "light",
"strengths": [
{
"id": "g1h1p1s1",
"strength": "guidence"
}
]
}
]
}
]
},
{
"id": "g2",
"group": "2",
"heroes": [
{
"id": "g2h1",
"hero": "hero1",
"powers": [
{
"id": "g2h1p1",
"power": "electromancy",
"strengths": [
{
"id": "g2h1p1s1",
"strength": "metal armor"
}
]
}
]
}
]
}
]

我已经尝试了很多尝试,但我只是没有得到正确的结果。以下是我的尝试之一。我究竟做错了什么?或者有更好的方法吗?

  filterGroups()
{
//Iterate groups
this.groups.forEach(group => {

//remove empty groups
if(group.heroes.length === 0)
{
let groupIndex: number = this.groups.findIndex(x => x.id === group.id);
this.groups.splice(groupIndex, 1);
}
else {
group.heroes.forEach(hero => {
//remove heroes with no powers
if(hero.powers.length === 0)
{
let heroIndex: number = group.heroes.findIndex(h => h.id === hero.id);
group.heroes.splice(heroIndex, 1);
}
else {
hero.powers.forEach(power => {
//remove powers with no strengths
if(power.strengths.length === 0)
{
let powerIndex: number = hero.powers.findIndex(p => p.id === power.id);
hero.powers.splice(powerIndex,1);
}
else {
power.strengths.forEach(strength => {
if(this.selectedStrengths.some(x => x === strength.id))
{
let strengthIndex: number = power.strengths.findIndex(s => s.id === strength.id);
if(strengthIndex>-1)
{
power.strengths.splice(strengthIndex,1);
}
}
})

//check if power is empty after filtering strengths
if(power.strengths.length === 0)
{
let powerIndex: number = hero.powers.findIndex(p => p.id === power.id);
hero.powers.splice(powerIndex,1);
}
}
});

//Check hero is empty after filtering powers
if(hero.powers.length === 0)
{
let heroIndex: number = group.heroes.findIndex(h => h.id === hero.id);
group.heroes.splice(heroIndex, 1);
}
}
});

//Check if group is empty after filtering heroes
if(group.heroes.length === 0)
{
let groupIndex: number = this.groups.findIndex(x => x.id === group.id);
this.groups.splice(groupIndex, 1);
}
}
});
}

最佳答案

    filterGroups() {
return this.groups.filter(g => {
g.heroes = g.heroes.filter(h => {
h.powers = h.powers.filter(p => {
p.strengths = p.strengths.filter(s => this.selectedStrengths.indexOf(s.id) === -1);
return p.strengths.length > 0
})
return h.powers.length > 0;
})
return g.heroes.length > 0
})
}

关于Javascript复杂数组过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195947/

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