Expressions/Scripts/Presets

Moderator: Paul Tuersley

 
ghaith
Topic Author
Posts: 13
Joined: Fri Oct 23, 2009 2:45 am

IndexOf problem

Mon Nov 02, 2009 12:20 pm

hi all
i know i have many questions but for now the most important is how to use String.indexOf()

i don't know why it is not working for me any idea .


thank you :shock:
 
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: Thu Jun 17, 2004 9:27 am
Location: New York City, NY
Contact:

Re: IndexOf problem

Mon Nov 02, 2009 1:35 pm

var str = "my string";

str.indexOf("m");    //result:  0
str.indexOf("s");    //result:  3
str.indexOf("t");    //result:  4
 
laracroft
Posts: 3
Joined: Tue Nov 10, 2009 12:22 pm

Re: IndexOf problem

Mon Nov 16, 2009 5:24 am

Hi Ghaith. I am Lara.

Lloydalvarez's code is working. You should try this.

Have a nice day.
 
User avatar
luigihmtz
Posts: 5
Joined: Wed Jul 10, 2013 5:56 pm
Location: Mexico City
Contact:

Re: IndexOf problem

Sun May 04, 2014 1:49 am

Hello, everyone.

It turns out that the indexOf method works fine with strings, but it fails when used with arrays, as opposed to regular javascript, which supports either.

For instance, this expression would cause an error in AE, as it shows Undefined because the variable contains an array. If you try this e.g. on "web" javascript, it won't break:

fruits = ["Banana", "Orange", "Apple", "Mango"];
a = fruits.indexOf("Apple");


But this one will always work because the variable contains a string, not an array:

numbers = "95? 10, 034";
a = numbers.indexOf("5");


So how can I retrieve an index number from some whole element (not an individual character) that is part of an array of strings by typing its full string? Just like my fruits example above.

BTW I'm using AECC. Cheers.
Last edited by luigihmtz on Mon May 05, 2014 3:22 pm, edited 3 times in total.
 
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: Thu Jun 17, 2004 9:27 am
Location: New York City, NY
Contact:

Re: IndexOf problem

Sun May 04, 2014 8:18 am

luigihmtz wrote:
It turns out that the indexOf method works fine with strings, but it fails when used with arrays, as opposed to regular javascript, which supports either.


IndexOf is a string function and doesn't work with arrays anywhere. To get an item in an array by index you simply do this:

myArray[3]; //gets the third item in the array


http://www.w3schools.com/js/js_obj_array.asp
 
User avatar
luigihmtz
Posts: 5
Joined: Wed Jul 10, 2013 5:56 pm
Location: Mexico City
Contact:

Re: IndexOf problem

Mon May 05, 2014 1:35 pm


IndexOf is a string function and doesn't work with arrays anywhere.



Sorry, my bad, I mixed up stuff.

I do know that indexOf has a string-only input parameter but, at least in regular javascript, it is NOT a string-only method. What I mean is that you can use either an array of strings or a mere string interchangeably along with indexOf.

That said, my setback remains true: After Effects appears to be capable of handling strings only, not arrays, regarding the thing whom indexOf is applied to. I also acknowledge that you can call any element by its index, but I need to do the opposite (hence the use of the indexOf), I mean, by bringing in a given string element I'll get its corresponding index number, which is the output i need.

I'm still trying to figure this out, but thanks for such a quick answer. :o
 
User avatar
luigihmtz
Posts: 5
Joined: Wed Jul 10, 2013 5:56 pm
Location: Mexico City
Contact:

Re: IndexOf problem

Mon May 05, 2014 1:37 pm

luigihmtz wrote:

IndexOf is a string function and doesn't work with arrays anywhere.



Sorry, my bad, I mixed up stuff.

I do know that indexOf has a string-only input parameter but, at least in regular javascript, it is NOT a string-only method. What I mean is that you can use either an array of strings or a mere string interchangeably along with indexOf.

That said, my setback remains true: After Effects appears to be capable of handling strings only, not arrays, regarding the thing whom indexOf is applied to. I also acknowledge that you can call any element by its index, but I want to do the opposite (hence the use of the indexOf), I mean, by bringing in a given string element I'll get its corresponding index number, which is the output i need.

I'm still trying to figure this out, but thanks for such a quick answer. :o
 
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: Thu Jun 17, 2004 9:27 am
Location: New York City, NY
Contact:

Re: IndexOf problem

Mon May 05, 2014 2:44 pm

After Effects scripting is "regular" javascript. They are only different in the extended methods. All the base Javascript is exactly the same, i think you are misunderstanding strings and arrays.
 
User avatar
luigihmtz
Posts: 5
Joined: Wed Jul 10, 2013 5:56 pm
Location: Mexico City
Contact:

Re: IndexOf problem

Mon May 05, 2014 3:15 pm

Hopefully this should make my point clearer:

For instance, this expression would cause an error in AE, as it shows Undefined because the variable contains an array. If you try this e.g. on "web" javascript, it won't break:

fruits = ["Banana", "Orange", "Apple", "Mango"];
a = fruits.indexOf("Apple");


But this one will always work because the variable contains a string, not an array:

numbers = "95? 10, 034";
a = numbers.indexOf("5");



Thanks!
 
User avatar
luigihmtz
Posts: 5
Joined: Wed Jul 10, 2013 5:56 pm
Location: Mexico City
Contact:

Re: IndexOf problem

Mon May 05, 2014 4:12 pm

OK, I came out with this:

feed = "Banana";
fruits = [ ];
fruits[0] = "Banana";
fruits[1] = "Orange";
fruits[2] = "Apple";
fruits[3] = "Mango";

for (var i=0; i<fruits.length; i++)
  {
   if ( feed == fruits[i] ){
     idx = i;
     break;
   } else { 0 }
}


Not quite elegant, but it works.
 
beginUndoGroup
Posts: 80
Joined: Tue Nov 27, 2012 6:41 am

Re: IndexOf problem

Tue May 06, 2014 2:06 pm

ExtendScript implements ECMA Standard 262 3rd Edition (December 1999), while the latest 'official' edition is the 5th (June 2011).
There are now plenty of new methods for arrays, like indexOf, every, forEach, map, filter, reduce, some...
But ExtendScript doesnt have them.

You can do something like this:
function myIndexOf(array, x){
   var n=-1, N=array.length;
   while (++n<N && array[n]!==x);
   return n<N ? n : -1;
   };

Who is online

Users browsing this forum: No registered users and 3 guests