nested forEach loops - error “forEach is not a function”





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







0















I had a simple for each, but realised I needed to get a different property of my JSON, so I need now to have 2 nested forEach loops, but I am not sure how to do this properly. I am getting an error saying my second forEach is not a function.



I had this code first:



getExample.then(function(response) {
response.data.forEach(function(x) {
x.friendlyName = x.AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


than I tried to change it to this:



response.data.forEach(function (x) {
x.accounttransferfinalitycategory.forEach(function (y) {
x.friendlyName = x.accounttransferfinalitycategory[y].AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


But I get an error now, what is the proper way to do this?










share|improve this question




















  • 1





    Probably x.accounttransferfinalitycategory is not an array!!!

    – Mamun
    Jan 3 at 11:23






  • 2





    please provide sample response data

    – shajji
    Jan 3 at 11:25






  • 1





    Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

    – Jonas Wilms
    Jan 3 at 11:26











  • we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

    – Robin Zigmond
    Jan 3 at 11:27


















0















I had a simple for each, but realised I needed to get a different property of my JSON, so I need now to have 2 nested forEach loops, but I am not sure how to do this properly. I am getting an error saying my second forEach is not a function.



I had this code first:



getExample.then(function(response) {
response.data.forEach(function(x) {
x.friendlyName = x.AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


than I tried to change it to this:



response.data.forEach(function (x) {
x.accounttransferfinalitycategory.forEach(function (y) {
x.friendlyName = x.accounttransferfinalitycategory[y].AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


But I get an error now, what is the proper way to do this?










share|improve this question




















  • 1





    Probably x.accounttransferfinalitycategory is not an array!!!

    – Mamun
    Jan 3 at 11:23






  • 2





    please provide sample response data

    – shajji
    Jan 3 at 11:25






  • 1





    Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

    – Jonas Wilms
    Jan 3 at 11:26











  • we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

    – Robin Zigmond
    Jan 3 at 11:27














0












0








0








I had a simple for each, but realised I needed to get a different property of my JSON, so I need now to have 2 nested forEach loops, but I am not sure how to do this properly. I am getting an error saying my second forEach is not a function.



I had this code first:



getExample.then(function(response) {
response.data.forEach(function(x) {
x.friendlyName = x.AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


than I tried to change it to this:



response.data.forEach(function (x) {
x.accounttransferfinalitycategory.forEach(function (y) {
x.friendlyName = x.accounttransferfinalitycategory[y].AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


But I get an error now, what is the proper way to do this?










share|improve this question
















I had a simple for each, but realised I needed to get a different property of my JSON, so I need now to have 2 nested forEach loops, but I am not sure how to do this properly. I am getting an error saying my second forEach is not a function.



I had this code first:



getExample.then(function(response) {
response.data.forEach(function(x) {
x.friendlyName = x.AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


than I tried to change it to this:



response.data.forEach(function (x) {
x.accounttransferfinalitycategory.forEach(function (y) {
x.friendlyName = x.accounttransferfinalitycategory[y].AccountTransferFinalityCategoryName + "-" + x.AccountTransferFinalityName;
});
});


But I get an error now, what is the proper way to do this?







javascript loops foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 11:31









dns_nx

1,56311736




1,56311736










asked Jan 3 at 11:21









Ana SequeiraAna Sequeira

129211




129211








  • 1





    Probably x.accounttransferfinalitycategory is not an array!!!

    – Mamun
    Jan 3 at 11:23






  • 2





    please provide sample response data

    – shajji
    Jan 3 at 11:25






  • 1





    Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

    – Jonas Wilms
    Jan 3 at 11:26











  • we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

    – Robin Zigmond
    Jan 3 at 11:27














  • 1





    Probably x.accounttransferfinalitycategory is not an array!!!

    – Mamun
    Jan 3 at 11:23






  • 2





    please provide sample response data

    – shajji
    Jan 3 at 11:25






  • 1





    Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

    – Jonas Wilms
    Jan 3 at 11:26











  • we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

    – Robin Zigmond
    Jan 3 at 11:27








1




1





Probably x.accounttransferfinalitycategory is not an array!!!

– Mamun
Jan 3 at 11:23





Probably x.accounttransferfinalitycategory is not an array!!!

– Mamun
Jan 3 at 11:23




2




2





please provide sample response data

– shajji
Jan 3 at 11:25





please provide sample response data

– shajji
Jan 3 at 11:25




1




1





Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

– Jonas Wilms
Jan 3 at 11:26





Is it x.AccountTransferFinalityCategoryName or x.accounttransferfinalitycategoryname ? That really matters ...

– Jonas Wilms
Jan 3 at 11:26













we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

– Robin Zigmond
Jan 3 at 11:27





we definitely need to see what the data is - but most likely x.accounttransferfinalitycategory is undefined, either because the response isn't what you expect, or you mis-spelled the property name

– Robin Zigmond
Jan 3 at 11:27












2 Answers
2






active

oldest

votes


















0














I'm not sure what exactly you want to say , but i think your problem is with applying forEach to object,
be sure that x.accounttransferfinalitycategory is array , if it is object try below code



x =  x.accounttransferfinalitycategory;
Object.keys(x).forEach(function (key){
console.log(x[key]);
});


or update your question with response and expected output :-)






share|improve this answer

































    -5














    I think you need this



    $.each(result, function () { console.log(this.Id); console.log(this.Value); });






    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%2f54021328%2fnested-foreach-loops-error-foreach-is-not-a-function%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I'm not sure what exactly you want to say , but i think your problem is with applying forEach to object,
      be sure that x.accounttransferfinalitycategory is array , if it is object try below code



      x =  x.accounttransferfinalitycategory;
      Object.keys(x).forEach(function (key){
      console.log(x[key]);
      });


      or update your question with response and expected output :-)






      share|improve this answer






























        0














        I'm not sure what exactly you want to say , but i think your problem is with applying forEach to object,
        be sure that x.accounttransferfinalitycategory is array , if it is object try below code



        x =  x.accounttransferfinalitycategory;
        Object.keys(x).forEach(function (key){
        console.log(x[key]);
        });


        or update your question with response and expected output :-)






        share|improve this answer




























          0












          0








          0







          I'm not sure what exactly you want to say , but i think your problem is with applying forEach to object,
          be sure that x.accounttransferfinalitycategory is array , if it is object try below code



          x =  x.accounttransferfinalitycategory;
          Object.keys(x).forEach(function (key){
          console.log(x[key]);
          });


          or update your question with response and expected output :-)






          share|improve this answer















          I'm not sure what exactly you want to say , but i think your problem is with applying forEach to object,
          be sure that x.accounttransferfinalitycategory is array , if it is object try below code



          x =  x.accounttransferfinalitycategory;
          Object.keys(x).forEach(function (key){
          console.log(x[key]);
          });


          or update your question with response and expected output :-)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 2 at 3:23









          Şivā SankĂr

          1,29811022




          1,29811022










          answered Jan 3 at 11:52









          HrishiHrishi

          4121215




          4121215

























              -5














              I think you need this



              $.each(result, function () { console.log(this.Id); console.log(this.Value); });






              share|improve this answer




























                -5














                I think you need this



                $.each(result, function () { console.log(this.Id); console.log(this.Value); });






                share|improve this answer


























                  -5












                  -5








                  -5







                  I think you need this



                  $.each(result, function () { console.log(this.Id); console.log(this.Value); });






                  share|improve this answer













                  I think you need this



                  $.each(result, function () { console.log(this.Id); console.log(this.Value); });







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 11:27









                  nrajsharma794nrajsharma794

                  92




                  92






























                      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%2f54021328%2fnested-foreach-loops-error-foreach-is-not-a-function%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

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      WPF add header to Image with URL pettitions [duplicate]