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"
{
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')
How can I filter my cards by the Object within the object?
CodePudding user response:
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.
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)