5 Replies Latest reply on Feb 16, 2012 11:12 AM by prodigalmaster

    remove elements from Array

    sensiblemind Level 1

      Hi all

      if i have array like

      var my_Arr:Array = new Array("a", "b", "c");

      and i have button to use random element of ths array , is there any to remove that element  that if i press the button again i won't find the last element ?

      could somebody help me out...?

      thnx

      Wael


        • 1. Re: remove elements from Array
          prodigalmaster Level 2

          var someArray:Array = [1,2,3,4,5,6,7,8,9];

          someArray.shift(); //remove first value (1)

          someArray.pop();//remove final value (9)

           

          if you type: someArray. then a list of possibilities will come up and you can see which each one does, e.g. .reverse probably reverses the array e.g. 9, 8, 7 ..etc.

          • 2. Re: remove elements from Array
            sensiblemind Level 1

            yes thank you , and also it help to undo the random selection to use reverse probably

            thnx prodigalmaster

            • 3. Re: remove elements from Array
              prodigalmaster Level 2

              No problem. Althought I don't see how reverse would undo the random selection at all.

              • 4. Re: remove elements from Array
                sensiblemind Level 1

                Actually you are right , i thought when i reverse the Array i could have Random value

                BUT is there a way to random the array and select item that i can remove it from my array ?

                here is the code i tried to make

                 

                var someArray:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

                 

                random_But.onRelease = function()

                {

                          trace(someArray.reverse());

                          txt = someArray[0];

                          trace("1st element   "    + someArray[0]);

                          someArray.shift();//remove first value (1)

                          //trace(someArray.length);

                          if (someArray.length <= 1)

                          {

                                    this.enabled = false;

                          }

                };

                i appreciate your Help

                • 5. Re: remove elements from Array
                  prodigalmaster Level 2

                  Make a variable and give it a random value with Math.round(Math.random()*randomNumber); or to simplify just use random(randomNumber).

                  Then remove a value from someArray using that variable.

                   

                  A quick google search showed that what you need is splice:

                  someArray.splice(randomNumber,1); //index of value in array, count of values to delete e.g. if it was 2 then it would deleted the value in the index and the next

                   

                  Therefore:

                   

                  var randomNumber = random(someArray.length);

                  var someArray:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

                   

                  random_But.onRelease = function() {

                  someArray.splice(randomNumber, 1);//removes 1 random element

                    /*

                  txt = someArray[0];

                            if (someArray.length <= 1)

                            {

                                      this.enabled = false;

                            }

                  */ //??

                  randomNumber = random(someArray.length);

                  };