Skip to main content
Known Participant
August 7, 2015
Answered

Remove spaces from array

  • August 7, 2015
  • 2 replies
  • 423 views

I have an array of 13 items (all of which are letters EX e,e,e,e ...).  It is being read in from a database.  However, I want to loop through the array and clean out any spaces that it might have.  I am having trouble doing this.  Anyone done this before?

Here is what I have so far.

outputBox.autoSize = TextFieldAutoSize.LEFT;

displayArray(system.selectedAnswers.length);

function displayArray(n:int):void {

    for (var i:int = 0; i < n; i++)

    {

  system.selectedAnswers.replace(/^\s+|\s+$/g, "");

  outputBox.appendText(String(system.selectedAnswers) + ",");

    }

}

Thanks.

This topic has been closed for replies.
Correct answer kglad

use a reverse for-loop:

function stripArrayF(a:Array):void{

for(var i:int=a.length-1;i>=0;i--){

a=a.split(' ').join('');

if(a.length==0){

a.splice(i,1);

}

}

}

2 replies

Known Participant
August 12, 2015

Thanks KGLAD

kglad
Community Expert
Community Expert
August 12, 2015

you're welcome.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 7, 2015

use a reverse for-loop:

function stripArrayF(a:Array):void{

for(var i:int=a.length-1;i>=0;i--){

a=a.split(' ').join('');

if(a.length==0){

a.splice(i,1);

}

}

}