0

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);
}
1

3 Answers 3

3

will be like this.

selected(event: MatAutocompleteSelectedEvent): void {
  var a = this.nameIdMap.get(event.option.viewValue);
  //this makes sure selected id does not exist
  if( this.arr.filter(x => /*makes cast int to string to compare items*/ String(a) == x).length < 1) {
      this.arr.push(String(a));
      this.fruits.push(event.option.viewValue);
  }
  console.log(a, this.arr);

  this.fruitInput.nativeElement.value = '';
  this.fruitCtrl.setValue(null);
}

make you sure arr variable is initialized since you declare it.

arr:any[] = [];

hope it be by helpful. D;

Sign up to request clarification or add additional context in comments.

Comments

2

Your array is declared, but never initialized. First you need to initialize it. And since you want it to be a string array you should declare it as such:

arr:string[] = [];

Then you need to convert your numbers to strings if you want to push them into that array. The push method doesn't return the array, so you have to keep referring to the field itself rather than capturing the result the way you're trying to do.

  this.arr.push(a.toString());
  var c = this.arr.map((input) => input).join(",").toString();
  console.log(c)

2 Comments

Thank you for your help ! I think your link is the same as the post. Push already works, now how do I pass this information to an array of strings?
I removed the link, since it's not necessary. Clarify your question: you don't "pass information" to an array.
0

let a = new Set();
a.add(String(1));
console.log(Array.from(a)); // ["1"]

2 Comments

Can you break it down to the minimal code example to illustrate the problem?
I have this in my demo, I can only get the ID´S correctly ... the rest of the code is commented because it is not working :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.