Get the index of the first element in an array with value greater than x










2















I have this array:



var array = [400, 4000, 400, 400, 4000];


How can I get the index of the first element with value greater than 400?



Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.

If there are duplicates of my question that apply to JS, I'd really want to see them.










share|improve this question


























    2















    I have this array:



    var array = [400, 4000, 400, 400, 4000];


    How can I get the index of the first element with value greater than 400?



    Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.

    If there are duplicates of my question that apply to JS, I'd really want to see them.










    share|improve this question
























      2












      2








      2


      3






      I have this array:



      var array = [400, 4000, 400, 400, 4000];


      How can I get the index of the first element with value greater than 400?



      Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.

      If there are duplicates of my question that apply to JS, I'd really want to see them.










      share|improve this question














      I have this array:



      var array = [400, 4000, 400, 400, 4000];


      How can I get the index of the first element with value greater than 400?



      Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.

      If there are duplicates of my question that apply to JS, I'd really want to see them.







      javascript arrays






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 12 '17 at 0:10









      someGuysomeGuy

      325




      325






















          2 Answers
          2






          active

          oldest

          votes


















          5














          You can use findIndex here



          check this snippet






          var array = [400, 4000, 400, 400, 4000];
          var index=array.findIndex(function(number)
          return number > 400;
          );
          console.log(index);








          share|improve this answer


















          • 3





            or in ES6 var index = array.findIndex(n => n > 400);

            – Alnitak
            Jan 12 '17 at 0:19











          • @RobG so it is - I misread the MDN page as saying it was in ES5

            – Alnitak
            Jan 12 '17 at 0:25







          • 1





            It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

            – RobG
            Jan 12 '17 at 0:27



















          3














          You can use a simple for loop and check each element.






          var array = [400, 4000, 400, 400, 4000];

          var result;

          for(var i=0, l=array.length; i<l; i++)
          if(array[i] > 400)
          result = i;
          break;



          if(typeof result !== 'undefined')
          console.log('number greater than 400 found at array index: ' + result);
          else
          console.log('no number greater than 400 found in the given arrry.');





          Read up: for - JavaScript | MDN






          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%2f41603036%2fget-the-index-of-the-first-element-in-an-array-with-value-greater-than-x%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









            5














            You can use findIndex here



            check this snippet






            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);








            share|improve this answer


















            • 3





              or in ES6 var index = array.findIndex(n => n > 400);

              – Alnitak
              Jan 12 '17 at 0:19











            • @RobG so it is - I misread the MDN page as saying it was in ES5

              – Alnitak
              Jan 12 '17 at 0:25







            • 1





              It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

              – RobG
              Jan 12 '17 at 0:27
















            5














            You can use findIndex here



            check this snippet






            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);








            share|improve this answer


















            • 3





              or in ES6 var index = array.findIndex(n => n > 400);

              – Alnitak
              Jan 12 '17 at 0:19











            • @RobG so it is - I misread the MDN page as saying it was in ES5

              – Alnitak
              Jan 12 '17 at 0:25







            • 1





              It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

              – RobG
              Jan 12 '17 at 0:27














            5












            5








            5







            You can use findIndex here



            check this snippet






            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);








            share|improve this answer













            You can use findIndex here



            check this snippet






            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);








            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);





            var array = [400, 4000, 400, 400, 4000];
            var index=array.findIndex(function(number)
            return number > 400;
            );
            console.log(index);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 12 '17 at 0:17









            GeekyGeeky

            6,00411237




            6,00411237







            • 3





              or in ES6 var index = array.findIndex(n => n > 400);

              – Alnitak
              Jan 12 '17 at 0:19











            • @RobG so it is - I misread the MDN page as saying it was in ES5

              – Alnitak
              Jan 12 '17 at 0:25







            • 1





              It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

              – RobG
              Jan 12 '17 at 0:27













            • 3





              or in ES6 var index = array.findIndex(n => n > 400);

              – Alnitak
              Jan 12 '17 at 0:19











            • @RobG so it is - I misread the MDN page as saying it was in ES5

              – Alnitak
              Jan 12 '17 at 0:25







            • 1





              It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

              – RobG
              Jan 12 '17 at 0:27








            3




            3





            or in ES6 var index = array.findIndex(n => n > 400);

            – Alnitak
            Jan 12 '17 at 0:19





            or in ES6 var index = array.findIndex(n => n > 400);

            – Alnitak
            Jan 12 '17 at 0:19













            @RobG so it is - I misread the MDN page as saying it was in ES5

            – Alnitak
            Jan 12 '17 at 0:25






            @RobG so it is - I misread the MDN page as saying it was in ES5

            – Alnitak
            Jan 12 '17 at 0:25





            1




            1





            It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

            – RobG
            Jan 12 '17 at 0:27






            It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)

            – RobG
            Jan 12 '17 at 0:27














            3














            You can use a simple for loop and check each element.






            var array = [400, 4000, 400, 400, 4000];

            var result;

            for(var i=0, l=array.length; i<l; i++)
            if(array[i] > 400)
            result = i;
            break;



            if(typeof result !== 'undefined')
            console.log('number greater than 400 found at array index: ' + result);
            else
            console.log('no number greater than 400 found in the given arrry.');





            Read up: for - JavaScript | MDN






            share|improve this answer





























              3














              You can use a simple for loop and check each element.






              var array = [400, 4000, 400, 400, 4000];

              var result;

              for(var i=0, l=array.length; i<l; i++)
              if(array[i] > 400)
              result = i;
              break;



              if(typeof result !== 'undefined')
              console.log('number greater than 400 found at array index: ' + result);
              else
              console.log('no number greater than 400 found in the given arrry.');





              Read up: for - JavaScript | MDN






              share|improve this answer



























                3












                3








                3







                You can use a simple for loop and check each element.






                var array = [400, 4000, 400, 400, 4000];

                var result;

                for(var i=0, l=array.length; i<l; i++)
                if(array[i] > 400)
                result = i;
                break;



                if(typeof result !== 'undefined')
                console.log('number greater than 400 found at array index: ' + result);
                else
                console.log('no number greater than 400 found in the given arrry.');





                Read up: for - JavaScript | MDN






                share|improve this answer















                You can use a simple for loop and check each element.






                var array = [400, 4000, 400, 400, 4000];

                var result;

                for(var i=0, l=array.length; i<l; i++)
                if(array[i] > 400)
                result = i;
                break;



                if(typeof result !== 'undefined')
                console.log('number greater than 400 found at array index: ' + result);
                else
                console.log('no number greater than 400 found in the given arrry.');





                Read up: for - JavaScript | MDN






                var array = [400, 4000, 400, 400, 4000];

                var result;

                for(var i=0, l=array.length; i<l; i++)
                if(array[i] > 400)
                result = i;
                break;



                if(typeof result !== 'undefined')
                console.log('number greater than 400 found at array index: ' + result);
                else
                console.log('no number greater than 400 found in the given arrry.');





                var array = [400, 4000, 400, 400, 4000];

                var result;

                for(var i=0, l=array.length; i<l; i++)
                if(array[i] > 400)
                result = i;
                break;



                if(typeof result !== 'undefined')
                console.log('number greater than 400 found at array index: ' + result);
                else
                console.log('no number greater than 400 found in the given arrry.');






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 18:22

























                answered Jan 12 '17 at 0:14









                Rahul DesaiRahul Desai

                11k1064106




                11k1064106



























                    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%2f41603036%2fget-the-index-of-the-first-element-in-an-array-with-value-greater-than-x%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3