The idea of this pattern is to manage different arrays. When user clicks on an option it selects/deselects it and option's value is pushed into/filtered from the corresponding array.
I am trying to write the universal method that will manage the array it was called with, at the moment it works fine with pushing values but doesn't work with filtering.
Angular Component Methods (doesn't work)
potatoesArray = [];
manageOptions($event, testingArray) {
const checkExistence = (array, value) => {
return !(array.indexOf(value) >= 0)
}
if ($event) {
// SPAN value
const optionValue = $event.target.innerText;
// Push if value isn't in array
if (checkExistence(testingArray, optionValue)) {
testingArray.push(optionValue);
// 'Remove' the value if it's in array
} else {
testingArray = testingArray.filter((option) => {
return option != optionValue;
})
}
}
Angular Component Methods (works if referenced to array directly)
potatoesArray = [];
manageOptions($event, testingArray) {
...
} else {
this.potatoesArray = testingArray.filter((option) => {
return option != optionValue;
})
}
}
Note
console.log(testingArray === this.potatoesArray) // true
Template implementation
<div class="option" (click)='manageOptions($event, this.potatoesArray)'>
<span>OPTION 1</span>
...
</div>
<div class="option" (click)='manageOptions($event, this.potatoesArray)'>
<span>OPTION 2</span>
...
</div>
<div class="option" (click)='manageOptions($event, this.potatoesArray)'>
<span>OPTION 3</span>
...
</div>