2 Replies Latest reply on Dec 8, 2011 3:17 AM by Jump_Over

    Maybe it was easy...

    Jump_Over Level 5

      Well, as a beginner I am trying to discover js step by step and all I have is forum, Google and stack of .pdfs

       

      Say, small problem, how to find index of specific Array's element...

       

      ...you can call myArray.findIndex("element") written on February 2, 2006

      ---> doesn't work today (findIndex is not a function)

       

      ...you can call toString(myArray).indexOf("element") written on Dec 18, 2009

      ---> doesn't work today. Result == -1 cause toString(myArray) gives a string "[object Global]"

       

      So, js has changed so much in back, that those kind of basic functions disappears

           or there are new ones, I can't google them?

       

      I know I can use statement for and catch index comparing myArray elements step by step, however...

      ...maybe it was easier at the beginning but Wind of Changes made a pretty mess here, from my position of course...

       

      can u help me? Thx


        • 1. Re: Maybe it was easy...
          [Jongware] Most Valuable Participant

          Jump_Over wrote:

           

          ...you can call myArray.findIndex("element") written on February 2, 2006

          ---> doesn't work today (findIndex is not a function)

           

           

          You dated it but where did you get it from? I found a reference where it's used (http://www.slideshare.net/thealoysiuns/javascript-forabsolutebeginnersgettingstarted) and right after that the function "findIndex" is defined (which, indeed, loops over each element with a for..loop).

           

           

          Jump_Over wrote:

           

          ...you can call toString(myArray).indexOf("element") written on Dec 18, 2009

          ---> doesn't work today. Result == -1 cause toString(myArray) gives a string "[object Global]"

           

          That depends on the kind of objects that are stored in myArray. Not everything can be fed into toString.

           

          I know I can use statement for and catch index comparing myArray elements step by step, however...

           

          Nothing wrong with that. It Just So Happens there is no built-in function, which should not put you off right from the start. In fact, at times I have found myself writing a function for something that was built-in after all.

           

          Okay. With that out of the way: much -- a lot, or perhaps everything -- depends on the type of your array objects and on what you intend to find. For example, if it's lots of text strings and you need to be able to find any one as fast as possible, you could sort them and use a binary search.

          1 person found this helpful
          • 2. Re: Maybe it was easy...
            Jump_Over Level 5

            Thank you Jongware,

            clean answer opens straight way to go.

             

            By the way..., http://www.guyfromchennai.com/?p=26 is written on February 2, 2006 by Kumar S.