Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

remove elements from Array

Explorer ,
Feb 13, 2012 Feb 13, 2012

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


TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Feb 13, 2012 Feb 13, 2012

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.

Translate
Engaged ,
Feb 13, 2012 Feb 13, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 14, 2012 Feb 14, 2012

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

thnx prodigalmaster

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 14, 2012 Feb 14, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 14, 2012 Feb 14, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 16, 2012 Feb 16, 2012
LATEST

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);

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines