• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Searching Arrays - indexOf

Community Expert ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting

Views

11.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Oct 08, 2013 Oct 08, 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

Votes

Translate

Translate
Adobe
Guru ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 08, 2013 Oct 08, 2013

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 09, 2013 Oct 09, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2013 Oct 09, 2013

Copy link to clipboard

Copied

Thanks, Davide.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

Can this then be used in JSX scripts as well? I actually tried it using include and adding ES5, but still nothing and doesnt run

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

LATEST

Ive found this solution from a different post here on the forum. Tested it and works

 

// IndexOf update to use number as well
// DBLjan
// https://community.adobe.com/t5/indesign-discussions/indexof-is-not-a-function/m-p/9050751#M52595
Array.prototype.indexOf = function ( item ) {
    var index = 0, length = this.length;
    for ( ; index < length; index++ ) {
            if ( this[index] === item )
                        return index;
    }
    return -1;
};

 

I post this so other perhps can use it when bumping into the same issue 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines