Ashad peal
Ashad peal

Reputation: 73

How to add checkbox selected items in a array - Angular

I have checkbox column in mat-table to select all or some items. I am getting the selected ids but struggling to put the selected userIDs in an array of object. How can I add the selected items in an array named userIDs?

The Ts file:

 userIDs: any[];
 sendRegretMail() {
  this.selection.selected.forEach(s => console.log(s.id));
  }

This is the body of my post request:enter image description here

Upvotes: 1

Views: 1012

Answers (2)

Suneel Kumar
Suneel Kumar

Reputation: 779

For your current solution

userIDs: any[];
sendRegretMail() {
  this.selection.selected.forEach(s => {
    console.log(s.id);
    userIDs.push({"id": s.id}); // Just push object of id with define array
  });
}

Another Solution

this.userIDs = this.selection.selected.map(o => ({id: o.id}));

Upvotes: 2

Snehasish Karmakar
Snehasish Karmakar

Reputation: 726

What about

userIDs = this.selection.selected.map(s => ({id: s.id}));

Upvotes: 1

Related Questions