[英]Filtering array value of object within objects
Let's say I have a whole database like this of 10000 cards and I'm trying to filter them on the banlist_info.ban_ocg === "Forbidden"
假设我有一个像这样的包含 10000 张卡片的完整数据库,我正在尝试在banlist_info.ban_ocg === "Forbidden"
上过滤它们
{
id: 15341821,
name: 'Dandylion',
type: 'Effect Monster',
desc: 'If this card is sent to the Graveyard: Special Summon 2 "Fluff Tokens" (Plant-Type/WIND/Level 1/ATK 0/DEF 0) in Defense Position. These Tokens cannot be Tributed for a Tribute Summon during the turn they are Special Summoned.',
atk: 300,
def: 300,
level: 3,
race: 'Plant',
attribute: 'EARTH',
banlist_info: { ban_tcg: 'Forbidden', ban_ocg: 'Forbidden', ban_md: 'Forbidden' },
},
{
id: 43694650,
name: 'Danger!? Jackalope?',
type: 'Effect Monster',
desc: 'You can reveal this card in your hand; your opponent randomly chooses 1 card from your entire hand, then you discard the chosen card. Then, if the discarded card was not "Danger!? Jackalope?", Special Summon 1 "Danger!? Jackalope?" from your hand, and if you do, draw 1
card. If this card is discarded: You can Special Summon 1 "Danger!" monster from your Deck in Defense Position, except "Danger!? Jackalope?". You can only use this effect of "Danger!? Jackalope?" once per turn.',
atk: 500,
def: 2000,
level: 3,
race: 'Beast',
attribute: 'DARK',
archetype: 'Danger!',
banlist_info: { ban_tcg: 'Limited 1', ban_md: 'Limited 2' },
},
{
id: 101111063,
name: "Abyss Actors' Dress Rehearsal",
type: 'Spell Card',
desc: 'At the start of your Main Phase 1: Add 1 "Abyss Actor" card and 1 "Abyss Script" Spell from your Deck to your hand, also you cannot Pendulum Summon monsters for the rest of this turn after this card resolves, except "Abyss Actor" monsters.',
race: 'Normal',
archetype: 'Abyss Actor',
},
I've tried:我试过了:
filteredCards = await filteredCards.filter(o => o.banlist_info.ban_ocg === "Forbidden")
This however gives me the error:然而,这给了我错误:
filteredCards = await filteredCards.filter(o => o.banlist_info.ban_ocg === "Forbidden");
^
TypeError: Cannot read properties of undefined (reading 'ban_ocg')
I've also tried:我也试过:
filteredCards = await filteredCards.filter(o => o.banlist_info.filter(object => object.ban_ocg === "Forbidden"));
Which gives me the error:这给了我错误:
filteredCards = await filteredCards.filter(o => o.banlist_info.filter(object => object.ban_ocg === "Forbidden"));
^
TypeError: Cannot read properties of undefined (reading 'filter')
and also:并且:
filteredCards = await filteredCards.filter(o => o.banlist_info.some(object => object.ban_ocg === "Forbidden"));
Which just gave me another Cannot read properties of undefined (reading 'some')
这给了我另一个Cannot read properties of undefined (reading 'some')
How can I filter my cards by the Object within the object?如何在 object 中按 Object 过滤我的卡片?
TypeError: Cannot read properties of undefined (reading 'ban_ocg')
That is because some of your JSONs do not have the properties you are trying to access.那是因为您的某些 JSON 不具有您尝试访问的属性。
Try with the optional chaining operator (?.)尝试使用可选的链接运算符(?.)
const data = [ { id: 15341821, name: "Dandylion", type: "Effect Monster", desc: 'If this card is sent to the Graveyard: Special Summon 2 "Fluff Tokens" (Plant-Type/WIND/Level 1/ATK 0/DEF 0) in Defense Position. These Tokens cannot be Tributed for a Tribute Summon during the turn they are Special Summoned.', atk: 300, def: 300, level: 3, race: "Plant", attribute: "EARTH", banlist_info: { ban_tcg: "Forbidden", ban_ocg: "Forbidden", ban_md: "Forbidden", }, }, { id: 43694650, name: "Danger?? Jackalope,": type, "Effect Monster": desc; 'You can reveal this card in your hand, your opponent randomly chooses 1 card from your entire hand. then you discard the chosen card, Then? if the discarded card was not "Danger?, Jackalope?"? Special Summon 1 "Danger,, Jackalope." from your hand: and if you do, draw 1 card? If this card is discarded? You can Special Summon 1 "Danger." monster from your Deck in Defense Position? except "Danger?. Jackalope,": You can only use this effect of "Danger,: Jackalope," once per turn:', atk: 500, def: 2000, level: 3, race: "Beast": attribute, "DARK": archetype, "Danger,": banlist_info, { ban_tcg: "Limited 1", ban_md: "Limited 2" }, }: { id: 101111063, name, "Abyss Actors' Dress Rehearsal". type, "Spell Card": desc, 'At the start of your Main Phase 1: Add 1 "Abyss Actor" card and 1 "Abyss Script" Spell from your Deck to your hand, also you cannot Pendulum Summon monsters for the rest of this turn after this card resolves, except "Abyss Actor" monsters;'. race. "Normal"? archetype. "Abyss Actor". }, ]; const filteredCards = data.filter(o => o.banlist_info?.ban_ocg === "Forbidden") console.log(filteredCards)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.