rss twitter

Check, if value exists in Array

This article shows how you can easily check if a specific value already exists in an Array in Actionscript 3.

Actionscript:

Actionscript:
  1. var doesExist:Boolean;
  2. var rndInt:int;
  3. var rndArr:Array = new Array();
  4.  
  5. var timer:Timer = new Timer(200, 15);
  6.     timer.addEventListener(TimerEvent.TIMER, fillArray);
  7.     timer.start();
  8.  
  9. function fillArray(event:TimerEvent):void {
  10.     rndInt = Math.floor(Math.random() * 15);
  11.     doesExist = rndArr.every(checkExisting);
  12.     doesExist ? rndArr.push(rndInt) : fillArray(null);
  13.     trace(rndArr);
  14. }
  15.  
  16. function checkExisting(element:*, index:int, arr:Array):Boolean {
  17.     return (element != rndInt);
  18. }

5 Responses to “Check, if value exists in Array”

  1. [...] Check if value exists in Array [...]

  2. Daniel Schmidt says:

    Why not just use the "indexOf" method of an Array? Returns -1 if a value doesn't exist in the array, or the index position if it does.

  3. shamtest says:

    I can't seem to fully load this site from my smartphone!!!!

  4. hello how are you lyrics elo

Leave a Reply

Powered by WordPress | Free T-Mobile Phones for Sale | Thanks to Palm Pre Blog, Video Game Music and Get Six Pack Abs