I have a array define like :
selectedDocumentID = [];
Next i have function when i get the ID, and other function where i give the id as parameter. When i click in element with id this id is going to this modifyArray. Next i check the id using console.log (its working and i get id). After that i create a new array list that include a selectedDocument array. (it work). And i want to push to list a id if it not exist or if exist just slice it from list...
modifyArray(id) {
let selectedDocumentID = [];
let id = 10;
console.log('this is: ' + id);
console.log(this.selectedDocumentID);
let list = this.selectedDocumentID;
let index = list.findIndex( x => x === id);
console.log(index);
if (index !== id) {
list.push(id);
console.log(list);
this.selectedDocumentID = list;
}
else {
list = list.slice(list.indexOf(id));
console.log(list);
this.selectedDocumentID = list;
}
}
So for exmaple. I click for document with id 2,4,5,6.
My selectedDocumentID = [2,4,5,6] and list also have [2,4,5,6].
Next i click to document with id 8 and my arrays = [2,4,5,6,8], after that i click to document with id 5 and my arrays you look like [2,4,6,8]. What i am doing wrong?