How can I remove a value from the array by clicking the checkbox in typescript

1

I have a checkbox which when clicked pushes in an array of name perm_telas, however when I select an already selected check again it is not correctly removing the array value.

I've tried something like:

  @ViewChildren('myItem') item; //Aqui é meu check
  perm_telas = []; //Aqui é meu array

  OnCheckboxSelect(id, event) { //Caso ocorra um check na tela, adiciona para o array perm_telas o id daquela tela.
    if (event.target.checked === true) {
      this.perm_telas.push(id);
      console.log(this.perm_telas);
    }
    if (event.target.checked === false) {//Caso clique em um já checado, retira aquele id do.
      this.perm_telas = this.perm_telas.filter((item) => id !== id);
    }
  }

The insertion is occurring correctly, I believe something is wrong with my pulling logic from the array.

    
asked by anonymous 31.07.2018 / 14:33

1 answer

1

When I want to remove a certain item from an object I do this:

Object

var array = ['A', 'B', 'F', 1, 2, 3, 5];

Look for 'F' within the object. It will return your index.

var index = array.indexOf('F');

If you find the value, remove.

if (index > -1) {
  array.splice(index, 1);
}

Where index is the position and 1 is the amount of items you want to remove, in this case, one.

In your case it would look like this:

var index = this.perm_telas.indexOf(id);

if (index > -1) {
    this.perm_telas.splice(index, 1);
}
    
31.07.2018 / 14:46