0

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?

3
  • I mean yeah I do understand that it doesn't work due to there being no .ban_ocg in some items of the database. I just didn't know how else I would filter it. Someone below has posted a good answer though which was to add a ? after the banlist_info which fixed my problem. Commented Nov 26, 2022 at 15:10
  • 1
    “due to there being no .ban_ocg in some items” — No! Read the error message very carefully. “Cannot read properties of undefined (reading 'ban_ocg')” — You cannot make JavaScript read properties from something that has the value undefined, or shorter: you cannot make JavaScript read properties of undefined. The error message tells you that this was encountered while trying to read ban_ocg in the expression o.banlist_info.ban_ocg. JavaScript cannot read the property ban_ocg of undefined. This means o.banlist_info is undefined, not .ban_ocg. Commented Nov 26, 2022 at 15:17
  • Oooh, that makes more sense. Thank you I'm still somewhat new to coding xD Commented Nov 26, 2022 at 18:39

1 Answer 1

2
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)

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need ? on the o just on banlist_info.
@Andy, Yes. updated the answer. unless there are no null or undefined entries in the array itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.