Skip to main content
Jump_Over
Legend
December 8, 2011
Question

Maybe it was easy...

  • December 8, 2011
  • 1 reply
  • 416 views

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


This topic has been closed for replies.

1 reply

Jongware
Community Expert
Community Expert
December 8, 2011

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.

Jump_Over
Jump_OverAuthor
Legend
December 8, 2011

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.