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


/ Published in: ActionScript 3
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.