Anna
Anna

Reputation: 1627

empty a control Array

Isn't there a way to emptyAll my control Array, i've tried doing it with a for loop :

console.log("primitif value : " + this.parameters.length);
      for (var index = 0; index < this.parameters.length; index++) {
          this.parameters.removeAt(this.parameters.length- index);
            console.log("seconde value :" + this.parameters.length);
      }

It's removing just the last value, i want to completely remove all elements, i can't neither do it like :

this.parameters = [];

Is there a way to do it ??

Upvotes: 1

Views: 185

Answers (2)

Colum
Colum

Reputation: 986

Create a cloned array of the parameters. Where you want to delete from the array, delete from the cloned one. Then call the cloned length. After the for loop set the parameters to the cloned.

console.log("primitif value : " + this.parameters.length);
    var clone = this.parameters;
    for (var index = 0; index < this.parameters.length; index++) {
        clone.removeAt(clone.length- index);
        console.log("seconde value :" + clone.length);
    }
    this.parameters = clone;

Using this solution means that if you need to manipulate anything in the parameters array or if an async method is going to try use one of these indexes they are still available until after the for loop.

Upvotes: 2

Verri
Verri

Reputation: 1608

Try this:

for (var index = this.parameters.length - 1; index >= 0; index--) {
      this.parameters.removeAt(index);
}

Upvotes: 3

Related Questions