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

Moderator: Paul Tuersley
Code: Select all
var str = "my string";
str.indexOf("m"); //result: 0
str.indexOf("s"); //result: 3
str.indexOf("t"); //result: 4
Code: Select all
fruits = ["Banana", "Orange", "Apple", "Mango"];
a = fruits.indexOf("Apple");
Code: Select all
numbers = "95? 10, 034";
a = numbers.indexOf("5");
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: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.
Code: Select all
myArray[3]; //gets the third item in the array
Sorry, my bad, I mixed up stuff.
IndexOf is a string function and doesn't work with arrays anywhere.
luigihmtz wrote:Sorry, my bad, I mixed up stuff.
IndexOf is a string function and doesn't work with arrays anywhere.
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.
Thanks!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:
But this one will always work because the variable contains a string, not an array:Code: Select all
fruits = ["Banana", "Orange", "Apple", "Mango"]; a = fruits.indexOf("Apple");
Code: Select all
numbers = "95? 10, 034"; a = numbers.indexOf("5");
Code: Select all
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 }
}
Code: Select all
function myIndexOf(array, x){
var n=-1, N=array.length;
while (++n<N && array[n]!==x);
return n<N ? n : -1;
};