I am using an autocomplete and I want to store the ID´s of the chosen users.
I want to store the IDs in an array of strings, and this array must only have unique values (cannot have duplicate values)
I tried to push and convert values (using tostring ()) but to no avail.
Can anyone get me some help?
The desired output (example): ["1", "2", "3", "4"]
My code DEMO
COMPONENT
ngOnInit() {
this.userService.getUsers().subscribe(
(val: any[]) =>{
this.allFruits = val.map(user => {
this.nameIdMap.set(user.username, user.id);
return user.username
});
this.fruitCtrl.setValue(null); //use this to apply changes instantly
}
)
}
remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
arr:any;
selected(event: MatAutocompleteSelectedEvent): void {
var a = this.nameIdMap.get(event.option.viewValue);
console.log(a);
// var b = this.arr.push(a);
// var c = b.map((input) => input).join(",").toString();
// console.log(c)
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}