Dynamically Change part of variable name?interface states and props in typescript reactHow get value datapicker in react toobox custom?Dynamically create components in ReactInitialize state with dynamic key based on props in reactJSmust be wrapped in an enclosing tagHow to send value to state using react bootstrap FormControl dropdown?Redux Form Wizard, button to send json field data?ModalFilterPicker load more on scrollHow to refactor React code to include less of 'this.state'What does React.component in React apart from creating instance of our component and setting props object on it?

Which meaning of "must" does the Slow spell use?

Is a Centaur PC considered an animal when calculating carrying capacity for vehicles?

Can a DM change an item given by another DM?

Find most "academic" implementation of doubly linked list

Will removing shelving screws from studs damage the studs?

Find feasible point in polynomial time in linear programming

How do I insert two edge loops equally spaced from the edges?

Why is there not a willingness from the world to step in between Pakistan and India?

Was a star-crossed lover

Drawing probabilities on a simplex in TikZ

Units in general relativity

Why did James Cameron decide to give Alita big eyes?

How to say "I only speak one which is English." in French?

Using a JoeBlow Sport pump on a presta valve

Dotted background on a flowchart

Federal Pacific 200a main panel problem with oversized 100a 2pole breaker

Why does Windows store Wi-Fi passwords in a reversible format?

How to pass 2>/dev/null as a variable?

Why did the population of Bhutan drop by 70% between 2007 and 2008?

Why was this commercial plane highly delayed mid-flight?

Biological refrigeration?

How could a self contained organic body propel itself in space

Shift lens vs move body?

To what extent should we fear giving offense?



Dynamically Change part of variable name?


interface states and props in typescript reactHow get value datapicker in react toobox custom?Dynamically create components in ReactInitialize state with dynamic key based on props in reactJSmust be wrapped in an enclosing tagHow to send value to state using react bootstrap FormControl dropdown?Redux Form Wizard, button to send json field data?ModalFilterPicker load more on scrollHow to refactor React code to include less of 'this.state'What does React.component in React apart from creating instance of our component and setting props object on it?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-3















In React, is it possible to dynamically change part of a variable name based on state?



For example, I'm feeding several components information from a JSON file. Depending on the chosen gender (male or female), I want to send the 'atlas' from either the "male_clothes.js" or the "female_clothes.js".



JSX Code:



class App extends Component {
constructor(props) {
super(props)
this.state =
current_gender: "male",
current_pants: 0,

<LimbSegment atlas=male_clothes[this.state.current_pants]['atlas']/>









share|improve this question


























  • so you want to change male_clothes ? depending on what state?

    – Alexandr Zavalii
    Mar 27 at 19:48

















-3















In React, is it possible to dynamically change part of a variable name based on state?



For example, I'm feeding several components information from a JSON file. Depending on the chosen gender (male or female), I want to send the 'atlas' from either the "male_clothes.js" or the "female_clothes.js".



JSX Code:



class App extends Component {
constructor(props) {
super(props)
this.state =
current_gender: "male",
current_pants: 0,

<LimbSegment atlas=male_clothes[this.state.current_pants]['atlas']/>









share|improve this question


























  • so you want to change male_clothes ? depending on what state?

    – Alexandr Zavalii
    Mar 27 at 19:48













-3












-3








-3








In React, is it possible to dynamically change part of a variable name based on state?



For example, I'm feeding several components information from a JSON file. Depending on the chosen gender (male or female), I want to send the 'atlas' from either the "male_clothes.js" or the "female_clothes.js".



JSX Code:



class App extends Component {
constructor(props) {
super(props)
this.state =
current_gender: "male",
current_pants: 0,

<LimbSegment atlas=male_clothes[this.state.current_pants]['atlas']/>









share|improve this question
















In React, is it possible to dynamically change part of a variable name based on state?



For example, I'm feeding several components information from a JSON file. Depending on the chosen gender (male or female), I want to send the 'atlas' from either the "male_clothes.js" or the "female_clothes.js".



JSX Code:



class App extends Component {
constructor(props) {
super(props)
this.state =
current_gender: "male",
current_pants: 0,

<LimbSegment atlas=male_clothes[this.state.current_pants]['atlas']/>






reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 20:21







Adam Weiler

















asked Mar 27 at 19:45









Adam WeilerAdam Weiler

961 silver badge9 bronze badges




961 silver badge9 bronze badges















  • so you want to change male_clothes ? depending on what state?

    – Alexandr Zavalii
    Mar 27 at 19:48

















  • so you want to change male_clothes ? depending on what state?

    – Alexandr Zavalii
    Mar 27 at 19:48
















so you want to change male_clothes ? depending on what state?

– Alexandr Zavalii
Mar 27 at 19:48





so you want to change male_clothes ? depending on what state?

– Alexandr Zavalii
Mar 27 at 19:48












3 Answers
3






active

oldest

votes


















1















const current_pants, gender = this.state // I assume gender is comming from the state
const clothes = gender === 'male' ? male_clothes : female_clothes
const atlas = clothes[current_pants].atlas
// Then:
<LimbSegment atlas=atlas/>





share|improve this answer
































    0















    not sure if I understood you correctly.
    So if the state.gender is male it will use male_clothes otherwise female_clothes



    <LimbSegment atlas=this.state.gender === 'male' ? 
    male_clothes[this.state.current_pants]['atlas'] :
    female_clothes[this.state.current_pants]['atlas']/>





    share|improve this answer
































      0















      Short one:



      <LimbSegment atlas=(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']/>


      But it is better to use more verbose approach:



      const clothes = this.state.someFlag ? male_clothes : female_clothes;
      ...
      <LimbSegment atlas=clothes[this.state.current_pants]['atlas']/>





      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%2f55385348%2fdynamically-change-part-of-variable-name%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1















        const current_pants, gender = this.state // I assume gender is comming from the state
        const clothes = gender === 'male' ? male_clothes : female_clothes
        const atlas = clothes[current_pants].atlas
        // Then:
        <LimbSegment atlas=atlas/>





        share|improve this answer





























          1















          const current_pants, gender = this.state // I assume gender is comming from the state
          const clothes = gender === 'male' ? male_clothes : female_clothes
          const atlas = clothes[current_pants].atlas
          // Then:
          <LimbSegment atlas=atlas/>





          share|improve this answer



























            1














            1










            1









            const current_pants, gender = this.state // I assume gender is comming from the state
            const clothes = gender === 'male' ? male_clothes : female_clothes
            const atlas = clothes[current_pants].atlas
            // Then:
            <LimbSegment atlas=atlas/>





            share|improve this answer













            const current_pants, gender = this.state // I assume gender is comming from the state
            const clothes = gender === 'male' ? male_clothes : female_clothes
            const atlas = clothes[current_pants].atlas
            // Then:
            <LimbSegment atlas=atlas/>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 19:51









            nico.amabilenico.amabile

            1721 silver badge10 bronze badges




            1721 silver badge10 bronze badges


























                0















                not sure if I understood you correctly.
                So if the state.gender is male it will use male_clothes otherwise female_clothes



                <LimbSegment atlas=this.state.gender === 'male' ? 
                male_clothes[this.state.current_pants]['atlas'] :
                female_clothes[this.state.current_pants]['atlas']/>





                share|improve this answer





























                  0















                  not sure if I understood you correctly.
                  So if the state.gender is male it will use male_clothes otherwise female_clothes



                  <LimbSegment atlas=this.state.gender === 'male' ? 
                  male_clothes[this.state.current_pants]['atlas'] :
                  female_clothes[this.state.current_pants]['atlas']/>





                  share|improve this answer



























                    0














                    0










                    0









                    not sure if I understood you correctly.
                    So if the state.gender is male it will use male_clothes otherwise female_clothes



                    <LimbSegment atlas=this.state.gender === 'male' ? 
                    male_clothes[this.state.current_pants]['atlas'] :
                    female_clothes[this.state.current_pants]['atlas']/>





                    share|improve this answer













                    not sure if I understood you correctly.
                    So if the state.gender is male it will use male_clothes otherwise female_clothes



                    <LimbSegment atlas=this.state.gender === 'male' ? 
                    male_clothes[this.state.current_pants]['atlas'] :
                    female_clothes[this.state.current_pants]['atlas']/>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 19:49









                    Alexandr ZavaliiAlexandr Zavalii

                    1,0701 gold badge9 silver badges26 bronze badges




                    1,0701 gold badge9 silver badges26 bronze badges
























                        0















                        Short one:



                        <LimbSegment atlas=(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']/>


                        But it is better to use more verbose approach:



                        const clothes = this.state.someFlag ? male_clothes : female_clothes;
                        ...
                        <LimbSegment atlas=clothes[this.state.current_pants]['atlas']/>





                        share|improve this answer





























                          0















                          Short one:



                          <LimbSegment atlas=(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']/>


                          But it is better to use more verbose approach:



                          const clothes = this.state.someFlag ? male_clothes : female_clothes;
                          ...
                          <LimbSegment atlas=clothes[this.state.current_pants]['atlas']/>





                          share|improve this answer



























                            0














                            0










                            0









                            Short one:



                            <LimbSegment atlas=(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']/>


                            But it is better to use more verbose approach:



                            const clothes = this.state.someFlag ? male_clothes : female_clothes;
                            ...
                            <LimbSegment atlas=clothes[this.state.current_pants]['atlas']/>





                            share|improve this answer













                            Short one:



                            <LimbSegment atlas=(this.state.someFlag ? male_clothes : female_clothes)[this.state.current_pants]['atlas']/>


                            But it is better to use more verbose approach:



                            const clothes = this.state.someFlag ? male_clothes : female_clothes;
                            ...
                            <LimbSegment atlas=clothes[this.state.current_pants]['atlas']/>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 27 at 19:51









                            UjinT34UjinT34

                            2,5001 gold badge3 silver badges16 bronze badges




                            2,5001 gold badge3 silver badges16 bronze badges






























                                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%2f55385348%2fdynamically-change-part-of-variable-name%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