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?
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
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
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
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.
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
Copy link to clipboard
Copied
Thanks, Davide.
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
Copy link to clipboard
Copied
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
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