How to filter by array of objects?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?

What's the point of deactivating Num Lock on login screens?

Convert two switches to a dual stack, and add outlet - possible here?

What does "Puller Prush Person" mean?

Is it inappropriate for a student to attend their mentor's dissertation defense?

Did Shadowfax go to Valinor?

What typically incentivizes a professor to change jobs to a lower ranking university?

Why is Minecraft giving an OpenGL error?

How is it possible to have an ability score that is less than 3?

Paid for article while in US on F-1 visa?

How does one intimidate enemies without having the capacity for violence?

Theorems that impeded progress

Do other languages have an "irreversible aspect"?

What does it mean to describe someone as a butt steak?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

Was any UN Security Council vote triple-vetoed?

Important Resources for Dark Age Civilizations?

Why can't we play rap on piano?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

dbcc cleantable batch size explanation

How much RAM could one put in a typical 80386 setup?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?



How to filter by array of objects?


How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















My JSON looks like this:




id: 1,
type: "Sword",
name: "ABC",
description: 'ABC',
rarities: [

rarity: 'Common',
damage: 100
,

rarity: 'Uncommon',
damage: 200
,

rarity: 'Rare',
damage: 300

]
...


and the following filtering code:



const string = "Search query";
const keys = ['type', 'name', 'rarity'];
this.setState(
data: data.filter(entry => keys.some(k => entry[k]
.toString()
.toLowerCase()
.includes(string)
))
)


which doesn't work and says Cannot read property 'toString' of undefined. It works if I remove the 'rarities' key but I am unable to get it to filter based on objects in the rarities array too. The idea is that the user should be able to search and filter a table where the type, name or rarity inside rarities equals the entered search value. Does someone know what im supposed to do, do I need to do additional fintering for objects within the array?










share|improve this question

















  • 1





    rarity is not a property of the entry object.

    – Emile Bergeron
    Mar 21 at 22:57












  • First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

    – Code-Apprentice
    Mar 21 at 22:58






  • 2





    It's not JSON - it's a JavaScript object.

    – Jack Bashford
    Mar 21 at 23:01

















0















My JSON looks like this:




id: 1,
type: "Sword",
name: "ABC",
description: 'ABC',
rarities: [

rarity: 'Common',
damage: 100
,

rarity: 'Uncommon',
damage: 200
,

rarity: 'Rare',
damage: 300

]
...


and the following filtering code:



const string = "Search query";
const keys = ['type', 'name', 'rarity'];
this.setState(
data: data.filter(entry => keys.some(k => entry[k]
.toString()
.toLowerCase()
.includes(string)
))
)


which doesn't work and says Cannot read property 'toString' of undefined. It works if I remove the 'rarities' key but I am unable to get it to filter based on objects in the rarities array too. The idea is that the user should be able to search and filter a table where the type, name or rarity inside rarities equals the entered search value. Does someone know what im supposed to do, do I need to do additional fintering for objects within the array?










share|improve this question

















  • 1





    rarity is not a property of the entry object.

    – Emile Bergeron
    Mar 21 at 22:57












  • First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

    – Code-Apprentice
    Mar 21 at 22:58






  • 2





    It's not JSON - it's a JavaScript object.

    – Jack Bashford
    Mar 21 at 23:01













0












0








0


0






My JSON looks like this:




id: 1,
type: "Sword",
name: "ABC",
description: 'ABC',
rarities: [

rarity: 'Common',
damage: 100
,

rarity: 'Uncommon',
damage: 200
,

rarity: 'Rare',
damage: 300

]
...


and the following filtering code:



const string = "Search query";
const keys = ['type', 'name', 'rarity'];
this.setState(
data: data.filter(entry => keys.some(k => entry[k]
.toString()
.toLowerCase()
.includes(string)
))
)


which doesn't work and says Cannot read property 'toString' of undefined. It works if I remove the 'rarities' key but I am unable to get it to filter based on objects in the rarities array too. The idea is that the user should be able to search and filter a table where the type, name or rarity inside rarities equals the entered search value. Does someone know what im supposed to do, do I need to do additional fintering for objects within the array?










share|improve this question














My JSON looks like this:




id: 1,
type: "Sword",
name: "ABC",
description: 'ABC',
rarities: [

rarity: 'Common',
damage: 100
,

rarity: 'Uncommon',
damage: 200
,

rarity: 'Rare',
damage: 300

]
...


and the following filtering code:



const string = "Search query";
const keys = ['type', 'name', 'rarity'];
this.setState(
data: data.filter(entry => keys.some(k => entry[k]
.toString()
.toLowerCase()
.includes(string)
))
)


which doesn't work and says Cannot read property 'toString' of undefined. It works if I remove the 'rarities' key but I am unable to get it to filter based on objects in the rarities array too. The idea is that the user should be able to search and filter a table where the type, name or rarity inside rarities equals the entered search value. Does someone know what im supposed to do, do I need to do additional fintering for objects within the array?







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 22:54









KosKos

31




31







  • 1





    rarity is not a property of the entry object.

    – Emile Bergeron
    Mar 21 at 22:57












  • First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

    – Code-Apprentice
    Mar 21 at 22:58






  • 2





    It's not JSON - it's a JavaScript object.

    – Jack Bashford
    Mar 21 at 23:01












  • 1





    rarity is not a property of the entry object.

    – Emile Bergeron
    Mar 21 at 22:57












  • First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

    – Code-Apprentice
    Mar 21 at 22:58






  • 2





    It's not JSON - it's a JavaScript object.

    – Jack Bashford
    Mar 21 at 23:01







1




1





rarity is not a property of the entry object.

– Emile Bergeron
Mar 21 at 22:57






rarity is not a property of the entry object.

– Emile Bergeron
Mar 21 at 22:57














First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

– Code-Apprentice
Mar 21 at 22:58





First of all, you are doing too much in a single line of code. You should break your code into multiple lines by assigning intermediate steps to variables. Then you can debug your code to see what's going on.

– Code-Apprentice
Mar 21 at 22:58




2




2





It's not JSON - it's a JavaScript object.

– Jack Bashford
Mar 21 at 23:01





It's not JSON - it's a JavaScript object.

– Jack Bashford
Mar 21 at 23:01












1 Answer
1






active

oldest

votes


















0














This is because when checking for the key rarity: entry[k] where k = rarity entry[rarity] , at this point



keys.some(k => entry[k]
.toString()
.toLowerCase()
.includes(string)


)



will look like:



keys.some(k => entry['rarity']
.toString()
.toLowerCase()
.includes(string)


)



entry['rarity'] doesn't exist so its undefined so basically its trying to do undefined.toString().... . rarity is nested inside rarities. So to access this you would have to be looking inside entry['rarities']['rarity']



You could try importing lodash https://lodash.com/ and then use lodash's _.get method to have something like :



const string = "Search query";
const keys = ['type', 'name', 'rarities.rarity'];
this.setState(
data: data.filter(entry => keys.some(k => _.get(entry, k)
.toString()
.toLowerCase()
.includes(string)
))
)


see that for your keys rarity is now rarities.rarity
this should work






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290431%2fhow-to-filter-by-array-of-objects%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is because when checking for the key rarity: entry[k] where k = rarity entry[rarity] , at this point



    keys.some(k => entry[k]
    .toString()
    .toLowerCase()
    .includes(string)


    )



    will look like:



    keys.some(k => entry['rarity']
    .toString()
    .toLowerCase()
    .includes(string)


    )



    entry['rarity'] doesn't exist so its undefined so basically its trying to do undefined.toString().... . rarity is nested inside rarities. So to access this you would have to be looking inside entry['rarities']['rarity']



    You could try importing lodash https://lodash.com/ and then use lodash's _.get method to have something like :



    const string = "Search query";
    const keys = ['type', 'name', 'rarities.rarity'];
    this.setState(
    data: data.filter(entry => keys.some(k => _.get(entry, k)
    .toString()
    .toLowerCase()
    .includes(string)
    ))
    )


    see that for your keys rarity is now rarities.rarity
    this should work






    share|improve this answer



























      0














      This is because when checking for the key rarity: entry[k] where k = rarity entry[rarity] , at this point



      keys.some(k => entry[k]
      .toString()
      .toLowerCase()
      .includes(string)


      )



      will look like:



      keys.some(k => entry['rarity']
      .toString()
      .toLowerCase()
      .includes(string)


      )



      entry['rarity'] doesn't exist so its undefined so basically its trying to do undefined.toString().... . rarity is nested inside rarities. So to access this you would have to be looking inside entry['rarities']['rarity']



      You could try importing lodash https://lodash.com/ and then use lodash's _.get method to have something like :



      const string = "Search query";
      const keys = ['type', 'name', 'rarities.rarity'];
      this.setState(
      data: data.filter(entry => keys.some(k => _.get(entry, k)
      .toString()
      .toLowerCase()
      .includes(string)
      ))
      )


      see that for your keys rarity is now rarities.rarity
      this should work






      share|improve this answer

























        0












        0








        0







        This is because when checking for the key rarity: entry[k] where k = rarity entry[rarity] , at this point



        keys.some(k => entry[k]
        .toString()
        .toLowerCase()
        .includes(string)


        )



        will look like:



        keys.some(k => entry['rarity']
        .toString()
        .toLowerCase()
        .includes(string)


        )



        entry['rarity'] doesn't exist so its undefined so basically its trying to do undefined.toString().... . rarity is nested inside rarities. So to access this you would have to be looking inside entry['rarities']['rarity']



        You could try importing lodash https://lodash.com/ and then use lodash's _.get method to have something like :



        const string = "Search query";
        const keys = ['type', 'name', 'rarities.rarity'];
        this.setState(
        data: data.filter(entry => keys.some(k => _.get(entry, k)
        .toString()
        .toLowerCase()
        .includes(string)
        ))
        )


        see that for your keys rarity is now rarities.rarity
        this should work






        share|improve this answer













        This is because when checking for the key rarity: entry[k] where k = rarity entry[rarity] , at this point



        keys.some(k => entry[k]
        .toString()
        .toLowerCase()
        .includes(string)


        )



        will look like:



        keys.some(k => entry['rarity']
        .toString()
        .toLowerCase()
        .includes(string)


        )



        entry['rarity'] doesn't exist so its undefined so basically its trying to do undefined.toString().... . rarity is nested inside rarities. So to access this you would have to be looking inside entry['rarities']['rarity']



        You could try importing lodash https://lodash.com/ and then use lodash's _.get method to have something like :



        const string = "Search query";
        const keys = ['type', 'name', 'rarities.rarity'];
        this.setState(
        data: data.filter(entry => keys.some(k => _.get(entry, k)
        .toString()
        .toLowerCase()
        .includes(string)
        ))
        )


        see that for your keys rarity is now rarities.rarity
        this should work







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 23:43









        Jonathan BeadleJonathan Beadle

        1043




        1043





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290431%2fhow-to-filter-by-array-of-objects%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript