Check if item is not already in an Array


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

Based upon another snippet from this site, but can't remember where i got it


Copy this code and paste it in your HTML
  1. private function isNewInArray(value:Object, arr:Array):Boolean
  2. {
  3. for (var i:uint=0; i < arr.length; i++)
  4. {
  5. if (arr[i]==value)
  6. {
  7. return false;
  8. }
  9. }
  10. return true;
  11. }
  12.  
  13. if(isNewInArray(foo, barArray))
  14. {
  15. barArray.push(foo);
  16. }
  17. else
  18. {
  19. trace("Item already in array");
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.