Posted By

adrianparr on 03/17/10


Tagged

number search array index Value position item as3 NaN


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

socialflasher
tspitzr


AS3 Search for a Value in Array and Return it's Position Index


 / Published in: ActionScript 3
 

If the item cannot be found then the return value is NaN. This function only returns the index of the first occurence found, so it's not very good if you have multiple occurrences of the same value in the array.

  1. function findIndexInArray(value:Object, arr:Array):Number {
  2. for (var i:uint=0; i < arr.length; i++) {
  3. if (arr[i]==value) {
  4. return i;
  5. }
  6. }
  7. return NaN;
  8. }
  9.  
  10. var myArray:Array = new Array("a","b","c","d","e","f","g");
  11. trace(findIndexInArray("c", myArray));
  12.  
  13. // OUTPUT
  14. // 2

Report this snippet  

You need to login to post a comment.