How can I push data into an array?












1















I have pushed some data into an array:



if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}


It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.










share|improve this question

























  • are you talking about errors array or form ?

    – Dhiren
    Nov 26 '18 at 12:00











  • about errors array

    – Jarla
    Nov 26 '18 at 12:01











  • looking at this tiny piece of code that you have shown doesn't need any optimization.

    – Dhiren
    Nov 26 '18 at 12:04











  • @Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

    – Colin Cline
    Nov 26 '18 at 12:06











  • @ColinCline correct.

    – Dhiren
    Nov 26 '18 at 12:07
















1















I have pushed some data into an array:



if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}


It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.










share|improve this question

























  • are you talking about errors array or form ?

    – Dhiren
    Nov 26 '18 at 12:00











  • about errors array

    – Jarla
    Nov 26 '18 at 12:01











  • looking at this tiny piece of code that you have shown doesn't need any optimization.

    – Dhiren
    Nov 26 '18 at 12:04











  • @Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

    – Colin Cline
    Nov 26 '18 at 12:06











  • @ColinCline correct.

    – Dhiren
    Nov 26 '18 at 12:07














1












1








1








I have pushed some data into an array:



if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}


It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.










share|improve this question
















I have pushed some data into an array:



if(target === 'create'){
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' || name === 'form[plainPassword]'){
errors.push(name);
}
} else {
if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ){
errors.push(name);
}
}


It actually works fine. But it seems to me that it is really too much repeating code, but I still cannot find a way to reduce the code, or make a simpler better solution.







javascript jquery if-statement push






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 12:16









Andrei Suvorkov

4,1674929




4,1674929










asked Nov 26 '18 at 11:58









JarlaJarla

2,35711639




2,35711639













  • are you talking about errors array or form ?

    – Dhiren
    Nov 26 '18 at 12:00











  • about errors array

    – Jarla
    Nov 26 '18 at 12:01











  • looking at this tiny piece of code that you have shown doesn't need any optimization.

    – Dhiren
    Nov 26 '18 at 12:04











  • @Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

    – Colin Cline
    Nov 26 '18 at 12:06











  • @ColinCline correct.

    – Dhiren
    Nov 26 '18 at 12:07



















  • are you talking about errors array or form ?

    – Dhiren
    Nov 26 '18 at 12:00











  • about errors array

    – Jarla
    Nov 26 '18 at 12:01











  • looking at this tiny piece of code that you have shown doesn't need any optimization.

    – Dhiren
    Nov 26 '18 at 12:04











  • @Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

    – Colin Cline
    Nov 26 '18 at 12:06











  • @ColinCline correct.

    – Dhiren
    Nov 26 '18 at 12:07

















are you talking about errors array or form ?

– Dhiren
Nov 26 '18 at 12:00





are you talking about errors array or form ?

– Dhiren
Nov 26 '18 at 12:00













about errors array

– Jarla
Nov 26 '18 at 12:01





about errors array

– Jarla
Nov 26 '18 at 12:01













looking at this tiny piece of code that you have shown doesn't need any optimization.

– Dhiren
Nov 26 '18 at 12:04





looking at this tiny piece of code that you have shown doesn't need any optimization.

– Dhiren
Nov 26 '18 at 12:04













@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

– Colin Cline
Nov 26 '18 at 12:06





@Dhiren Indeed, In fact, it could be shorter but it's just an if/else that cost nothing in programming languages. So there is no need to be optimized

– Colin Cline
Nov 26 '18 at 12:06













@ColinCline correct.

– Dhiren
Nov 26 '18 at 12:07





@ColinCline correct.

– Dhiren
Nov 26 '18 at 12:07












3 Answers
3






active

oldest

votes


















1














If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:



if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' 
|| (name === 'form[plainPassword]' && target === 'create')){
errors.push(name);
}


Since:



name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'


is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.



In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all






share|improve this answer

































    2














    I would do it in two methods



    function CheckErrors(target,name){
    switch(target){
    case 'create':
    SaveError(name,true);
    break;
    default:
    SaveError(name);
    break;
    }
    }

    function SaveError(name,checkPassword){
    if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
    errors.push(name);
    }
    }





    share|improve this answer


























    • I am afraid you missed something

      – Colin Cline
      Nov 26 '18 at 12:07











    • just trying to figure out what i missed :)

      – JustLearning
      Nov 26 '18 at 12:09











    • OP dont want to push 'form[plainPassword]' in else statement

      – Colin Cline
      Nov 26 '18 at 12:11






    • 1





      indeed i see it. If i had to re-write it would still look ugly

      – JustLearning
      Nov 26 '18 at 12:13



















    2














    You could use arrays to simplify your if statement:



    if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
    errors.push(name);
    }





    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%2f53480652%2fhow-can-i-push-data-into-an-array%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














      If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:



      if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' 
      || (name === 'form[plainPassword]' && target === 'create')){
      errors.push(name);
      }


      Since:



      name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'


      is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.



      In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all






      share|improve this answer






























        1














        If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:



        if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' 
        || (name === 'form[plainPassword]' && target === 'create')){
        errors.push(name);
        }


        Since:



        name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'


        is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.



        In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all






        share|improve this answer




























          1












          1








          1







          If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:



          if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' 
          || (name === 'form[plainPassword]' && target === 'create')){
          errors.push(name);
          }


          Since:



          name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'


          is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.



          In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all






          share|improve this answer















          If I rigth understand you, you want to simplify your statements. From my perspective it could be like this:



          if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' 
          || (name === 'form[plainPassword]' && target === 'create')){
          errors.push(name);
          }


          Since:



          name === 'form[username]' || name === 'form[name]' || name === 'form[slug]'


          is repeated two times, it doesn't matter if target === 'create' is true or false for this statements.



          In fact just add (name === 'form[plainPassword]' && target === 'create') to if statement and that's all







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 12:09

























          answered Nov 26 '18 at 12:03









          Andrei SuvorkovAndrei Suvorkov

          4,1674929




          4,1674929

























              2














              I would do it in two methods



              function CheckErrors(target,name){
              switch(target){
              case 'create':
              SaveError(name,true);
              break;
              default:
              SaveError(name);
              break;
              }
              }

              function SaveError(name,checkPassword){
              if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
              errors.push(name);
              }
              }





              share|improve this answer


























              • I am afraid you missed something

                – Colin Cline
                Nov 26 '18 at 12:07











              • just trying to figure out what i missed :)

                – JustLearning
                Nov 26 '18 at 12:09











              • OP dont want to push 'form[plainPassword]' in else statement

                – Colin Cline
                Nov 26 '18 at 12:11






              • 1





                indeed i see it. If i had to re-write it would still look ugly

                – JustLearning
                Nov 26 '18 at 12:13
















              2














              I would do it in two methods



              function CheckErrors(target,name){
              switch(target){
              case 'create':
              SaveError(name,true);
              break;
              default:
              SaveError(name);
              break;
              }
              }

              function SaveError(name,checkPassword){
              if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
              errors.push(name);
              }
              }





              share|improve this answer


























              • I am afraid you missed something

                – Colin Cline
                Nov 26 '18 at 12:07











              • just trying to figure out what i missed :)

                – JustLearning
                Nov 26 '18 at 12:09











              • OP dont want to push 'form[plainPassword]' in else statement

                – Colin Cline
                Nov 26 '18 at 12:11






              • 1





                indeed i see it. If i had to re-write it would still look ugly

                – JustLearning
                Nov 26 '18 at 12:13














              2












              2








              2







              I would do it in two methods



              function CheckErrors(target,name){
              switch(target){
              case 'create':
              SaveError(name,true);
              break;
              default:
              SaveError(name);
              break;
              }
              }

              function SaveError(name,checkPassword){
              if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
              errors.push(name);
              }
              }





              share|improve this answer















              I would do it in two methods



              function CheckErrors(target,name){
              switch(target){
              case 'create':
              SaveError(name,true);
              break;
              default:
              SaveError(name);
              break;
              }
              }

              function SaveError(name,checkPassword){
              if(name === 'form[username]' || name === 'form[name]' || name === 'form[slug]' ||(checkPassword && name === 'form[plainPassword]')){
              errors.push(name);
              }
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 12:14

























              answered Nov 26 '18 at 12:07









              JustLearningJustLearning

              1,05821636




              1,05821636













              • I am afraid you missed something

                – Colin Cline
                Nov 26 '18 at 12:07











              • just trying to figure out what i missed :)

                – JustLearning
                Nov 26 '18 at 12:09











              • OP dont want to push 'form[plainPassword]' in else statement

                – Colin Cline
                Nov 26 '18 at 12:11






              • 1





                indeed i see it. If i had to re-write it would still look ugly

                – JustLearning
                Nov 26 '18 at 12:13



















              • I am afraid you missed something

                – Colin Cline
                Nov 26 '18 at 12:07











              • just trying to figure out what i missed :)

                – JustLearning
                Nov 26 '18 at 12:09











              • OP dont want to push 'form[plainPassword]' in else statement

                – Colin Cline
                Nov 26 '18 at 12:11






              • 1





                indeed i see it. If i had to re-write it would still look ugly

                – JustLearning
                Nov 26 '18 at 12:13

















              I am afraid you missed something

              – Colin Cline
              Nov 26 '18 at 12:07





              I am afraid you missed something

              – Colin Cline
              Nov 26 '18 at 12:07













              just trying to figure out what i missed :)

              – JustLearning
              Nov 26 '18 at 12:09





              just trying to figure out what i missed :)

              – JustLearning
              Nov 26 '18 at 12:09













              OP dont want to push 'form[plainPassword]' in else statement

              – Colin Cline
              Nov 26 '18 at 12:11





              OP dont want to push 'form[plainPassword]' in else statement

              – Colin Cline
              Nov 26 '18 at 12:11




              1




              1





              indeed i see it. If i had to re-write it would still look ugly

              – JustLearning
              Nov 26 '18 at 12:13





              indeed i see it. If i had to re-write it would still look ugly

              – JustLearning
              Nov 26 '18 at 12:13











              2














              You could use arrays to simplify your if statement:



              if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
              errors.push(name);
              }





              share|improve this answer




























                2














                You could use arrays to simplify your if statement:



                if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
                errors.push(name);
                }





                share|improve this answer


























                  2












                  2








                  2







                  You could use arrays to simplify your if statement:



                  if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
                  errors.push(name);
                  }





                  share|improve this answer













                  You could use arrays to simplify your if statement:



                  if((target === 'create' && name === 'form[plainPassword]') || ['form[username]', 'form[name]', 'form[slug]'].includes(name)){
                  errors.push(name);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 12:18









                  XorifelseXorifelse

                  6,48311934




                  6,48311934






























                      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%2f53480652%2fhow-can-i-push-data-into-an-array%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

                      Lallio

                      Futebolista

                      Jornalista