Iterate over an array of objects with object having multiple properties












3














I'm trying to iterate over the following:



state = {
products : [
{
x : "sd",
y : "fdg"
}, {
x : "sdx",
y : "fdgx"
}
]
}


I need to iterate over the above products array and inside object to create:



<tr><td>sd</td><td>fdg</td></tr>


I tried using :



{
this.state.products.map(function(prod) {

return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
})
}


but get multiple errors and prod being undefined.










share|improve this question




















  • 1




    @CertainPerformance updated code
    – the_legend_27
    Nov 21 '18 at 1:55
















3














I'm trying to iterate over the following:



state = {
products : [
{
x : "sd",
y : "fdg"
}, {
x : "sdx",
y : "fdgx"
}
]
}


I need to iterate over the above products array and inside object to create:



<tr><td>sd</td><td>fdg</td></tr>


I tried using :



{
this.state.products.map(function(prod) {

return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
})
}


but get multiple errors and prod being undefined.










share|improve this question




















  • 1




    @CertainPerformance updated code
    – the_legend_27
    Nov 21 '18 at 1:55














3












3








3







I'm trying to iterate over the following:



state = {
products : [
{
x : "sd",
y : "fdg"
}, {
x : "sdx",
y : "fdgx"
}
]
}


I need to iterate over the above products array and inside object to create:



<tr><td>sd</td><td>fdg</td></tr>


I tried using :



{
this.state.products.map(function(prod) {

return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
})
}


but get multiple errors and prod being undefined.










share|improve this question















I'm trying to iterate over the following:



state = {
products : [
{
x : "sd",
y : "fdg"
}, {
x : "sdx",
y : "fdgx"
}
]
}


I need to iterate over the above products array and inside object to create:



<tr><td>sd</td><td>fdg</td></tr>


I tried using :



{
this.state.products.map(function(prod) {

return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
})
}


but get multiple errors and prod being undefined.







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 2:02









Dacre Denny

10.5k4929




10.5k4929










asked Nov 21 '18 at 1:50









the_legend_27

33119




33119








  • 1




    @CertainPerformance updated code
    – the_legend_27
    Nov 21 '18 at 1:55














  • 1




    @CertainPerformance updated code
    – the_legend_27
    Nov 21 '18 at 1:55








1




1




@CertainPerformance updated code
– the_legend_27
Nov 21 '18 at 1:55




@CertainPerformance updated code
– the_legend_27
Nov 21 '18 at 1:55












3 Answers
3






active

oldest

votes


















0














It's possible that logic elsewhere in your component is mutating the state, which in turn may be the root cause of the error thrown during rendering.



Be sure to check that the products array is consistently defined in your components state, and that the items in that array are defined.



One solution here might be to take a more defensive approach to rendering your table row elements, by doing the following:



{
Array.isArray(this.state.products) && this.state.products
.filter(product => !!product)
.map(product => {

return <tr><td>{product.x}</td><td>{product.y}</td></tr>;
})
}


Here, the rendering logic asserts that this.state.products is the expected array type via Array.isArray(). Addtionally, the logic ensures any prop being rendered is defined by first filtering out any undefined prop items via this line:



filter(product => !!product)


Hope that helps!






share|improve this answer































    0














    The problem is that the return statement is an HTML code which is causing the problem whereas you can encode the code into a string and the DOM will treat it as HTML code



    this.state.products.map(function(prod){  return "<tr><td>"+prod.x+"</td><td>"+prod.y +"</td> </tr>" }).





    share|improve this answer





























      0














      you need to add that in one variable return as below:



      const prod = this.state.products.map(function(prod) { 
      return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
      });


      Use the variable inside render lifecycle as below.



      {prod}


      Here is the working code attached in jsFiddle



      Hope this helps!






      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%2f53404238%2fiterate-over-an-array-of-objects-with-object-having-multiple-properties%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









        0














        It's possible that logic elsewhere in your component is mutating the state, which in turn may be the root cause of the error thrown during rendering.



        Be sure to check that the products array is consistently defined in your components state, and that the items in that array are defined.



        One solution here might be to take a more defensive approach to rendering your table row elements, by doing the following:



        {
        Array.isArray(this.state.products) && this.state.products
        .filter(product => !!product)
        .map(product => {

        return <tr><td>{product.x}</td><td>{product.y}</td></tr>;
        })
        }


        Here, the rendering logic asserts that this.state.products is the expected array type via Array.isArray(). Addtionally, the logic ensures any prop being rendered is defined by first filtering out any undefined prop items via this line:



        filter(product => !!product)


        Hope that helps!






        share|improve this answer




























          0














          It's possible that logic elsewhere in your component is mutating the state, which in turn may be the root cause of the error thrown during rendering.



          Be sure to check that the products array is consistently defined in your components state, and that the items in that array are defined.



          One solution here might be to take a more defensive approach to rendering your table row elements, by doing the following:



          {
          Array.isArray(this.state.products) && this.state.products
          .filter(product => !!product)
          .map(product => {

          return <tr><td>{product.x}</td><td>{product.y}</td></tr>;
          })
          }


          Here, the rendering logic asserts that this.state.products is the expected array type via Array.isArray(). Addtionally, the logic ensures any prop being rendered is defined by first filtering out any undefined prop items via this line:



          filter(product => !!product)


          Hope that helps!






          share|improve this answer


























            0












            0








            0






            It's possible that logic elsewhere in your component is mutating the state, which in turn may be the root cause of the error thrown during rendering.



            Be sure to check that the products array is consistently defined in your components state, and that the items in that array are defined.



            One solution here might be to take a more defensive approach to rendering your table row elements, by doing the following:



            {
            Array.isArray(this.state.products) && this.state.products
            .filter(product => !!product)
            .map(product => {

            return <tr><td>{product.x}</td><td>{product.y}</td></tr>;
            })
            }


            Here, the rendering logic asserts that this.state.products is the expected array type via Array.isArray(). Addtionally, the logic ensures any prop being rendered is defined by first filtering out any undefined prop items via this line:



            filter(product => !!product)


            Hope that helps!






            share|improve this answer














            It's possible that logic elsewhere in your component is mutating the state, which in turn may be the root cause of the error thrown during rendering.



            Be sure to check that the products array is consistently defined in your components state, and that the items in that array are defined.



            One solution here might be to take a more defensive approach to rendering your table row elements, by doing the following:



            {
            Array.isArray(this.state.products) && this.state.products
            .filter(product => !!product)
            .map(product => {

            return <tr><td>{product.x}</td><td>{product.y}</td></tr>;
            })
            }


            Here, the rendering logic asserts that this.state.products is the expected array type via Array.isArray(). Addtionally, the logic ensures any prop being rendered is defined by first filtering out any undefined prop items via this line:



            filter(product => !!product)


            Hope that helps!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 2:32

























            answered Nov 21 '18 at 2:09









            Dacre Denny

            10.5k4929




            10.5k4929

























                0














                The problem is that the return statement is an HTML code which is causing the problem whereas you can encode the code into a string and the DOM will treat it as HTML code



                this.state.products.map(function(prod){  return "<tr><td>"+prod.x+"</td><td>"+prod.y +"</td> </tr>" }).





                share|improve this answer


























                  0














                  The problem is that the return statement is an HTML code which is causing the problem whereas you can encode the code into a string and the DOM will treat it as HTML code



                  this.state.products.map(function(prod){  return "<tr><td>"+prod.x+"</td><td>"+prod.y +"</td> </tr>" }).





                  share|improve this answer
























                    0












                    0








                    0






                    The problem is that the return statement is an HTML code which is causing the problem whereas you can encode the code into a string and the DOM will treat it as HTML code



                    this.state.products.map(function(prod){  return "<tr><td>"+prod.x+"</td><td>"+prod.y +"</td> </tr>" }).





                    share|improve this answer












                    The problem is that the return statement is an HTML code which is causing the problem whereas you can encode the code into a string and the DOM will treat it as HTML code



                    this.state.products.map(function(prod){  return "<tr><td>"+prod.x+"</td><td>"+prod.y +"</td> </tr>" }).






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 6:00









                    Advait

                    215




                    215























                        0














                        you need to add that in one variable return as below:



                        const prod = this.state.products.map(function(prod) { 
                        return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
                        });


                        Use the variable inside render lifecycle as below.



                        {prod}


                        Here is the working code attached in jsFiddle



                        Hope this helps!






                        share|improve this answer


























                          0














                          you need to add that in one variable return as below:



                          const prod = this.state.products.map(function(prod) { 
                          return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
                          });


                          Use the variable inside render lifecycle as below.



                          {prod}


                          Here is the working code attached in jsFiddle



                          Hope this helps!






                          share|improve this answer
























                            0












                            0








                            0






                            you need to add that in one variable return as below:



                            const prod = this.state.products.map(function(prod) { 
                            return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
                            });


                            Use the variable inside render lifecycle as below.



                            {prod}


                            Here is the working code attached in jsFiddle



                            Hope this helps!






                            share|improve this answer












                            you need to add that in one variable return as below:



                            const prod = this.state.products.map(function(prod) { 
                            return <tr><td>{prod.x}</td><td>{prod.y}</td></tr>;
                            });


                            Use the variable inside render lifecycle as below.



                            {prod}


                            Here is the working code attached in jsFiddle



                            Hope this helps!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 6:42









                            varit05

                            1,630715




                            1,630715






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53404238%2fiterate-over-an-array-of-objects-with-object-having-multiple-properties%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

                                Create new schema in PostgreSQL using DBeaver

                                Deepest pit of an array with Javascript: test on Codility

                                Costa Masnaga