Skip to main content
Chuck Uebele
Community Expert
Community Expert
October 8, 2013
Answered

Searching Arrays - indexOf

  • October 8, 2013
  • 3 replies
  • 12569 views

I was trying to search an array, and on the web it says to use something like this:

var tA = ['one','two'];

$.writeln(tA.indexOf ('one'))

However, when I try this, I get: tA.indexOf is not a function."  It works with a string variable but not an array.  Is this a bug, or an I doing something wrong?

This topic has been closed for replies.
Correct answer Michael_L_Hale

What you have found it one of the differences between ExtendScript and JavaScript. For the most part ExtendScript only supports the origianl core class methods. The indexOf method for Arrays was added to JavaScript later. However you can extend the array class in ExtendScript to add that method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

3 replies

tarekahf
Inspiring
May 10, 2016

Just to add that "array.indexOf(value)" worked well for me on the client side, but on the server side it gave me "indexOf is not a function" error. So, I just used array.join(",").indexOf(value) and it it worked like a charm.

Tarek

pixxxelschubser
Community Expert
Community Expert
October 8, 2013

I think no chance.

IndexOf is a method of Javascript 1.6 – but not of "Adobe-Javascript".

Look at the OMV for the possible methods of an array.

Try something like this instead:

var tA = new Array("one", "two", "three", "four");

var yourIndexOf = "four";

for(var i = 0; i < tA.length; i++) {

    if(tA === yourIndexOf) {

        result = i;

        i = tA.length;

        }

    }

alert (result);

Maybe someone knows a better way.

<Edit>

After seeing the post of Michael L Hale – here is the better way:

var tA = new Array("one", "two", "three", "four");

if (typeof Array.prototype.indexOf != "function") {

    Array.prototype.indexOf = function (el) {

        for(var i = 0; i < this.length; i++) if(el === this) return i;

        return -1;

        }

}

$.writeln (tA.indexOf ("two"))

Nachricht geändert durch pixxxel schubser

Chuck Uebele
Community Expert
Community Expert
October 9, 2013

Thanks Mike & pixxxel schubser.

I did end up using the loop to go through the array, but Mike's way looks good.  I thought there was some way to do this, as I seem to recall a way of finding the datetimeoriginal in the exif info without having to loop, but this is not what I was trying to do.

DBarranca
Legend
October 9, 2013

Chuck,

I've been using this shims collection to emulate ES5 on ExtendScript: besides Date related function (toISOString for instance), everything seems to work properly.

Beware minified versions, they are rather problematic (see this analysis), in my experience it's better to stick to uncompressed code.

Davide

www.davidebarranca.com

Michael_L_HaleCorrect answer
Inspiring
October 8, 2013

What you have found it one of the differences between ExtendScript and JavaScript. For the most part ExtendScript only supports the origianl core class methods. The indexOf method for Arrays was added to JavaScript later. However you can extend the array class in ExtendScript to add that method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf