【问题标题】:how to get list of ids to string object with comma in angular如何获取 ids 列表以使用 Angular 中的逗号字符串对象
【发布时间】:2020-01-03 20:25:14
【问题描述】:

我已经下拉了

 userList =[ { id: "0",name: "ALL"},{id: "1",name: "Smith"},{id: "2",name: "Smith"},{
 id: "3", name: "RAM"}];

when select ALL option i need to get all id's of user into string object.

string str = {1,2,3}(不包括0)

 <mat-select placeholder="User"(ngModelChange)="modelChanged($event)" formControlName="user" name="user">
  <mat-option *ngFor="let data of userList" [value]="data.id">{{data.name}}  </mat-option>
  </mat-select>

我的打字稿:

 onChangeRole(): void {
        this.userRoleService.getUserDetails().subscribe((response: Response) => {
            if (response.failure) {
                this.modalPopupService.openPopup(AlertComponent, { message: response.error });
            } else {
                this.userList = response.result;
                const all = {
                    id:  0,
                    name:  'ALL'
                   };
                   this.userList.splice(0, 0, all);
            }
        });
    }

modelChanged(res): void {
        const userType = res;
        if (userType === 0) {
         this.userType = ???;
        } else {
            this.userType = userType;
        }
    }

this.usertype 之上是任何属性。

如何将 id 列表放入字符串对象中?

【问题讨论】:

    标签: arrays filter angular-material angular7 dropdown


    【解决方案1】:

    你可以filter你的数组然后map只是id属性:

    userList.filter(f => f.id != zeroSymbol).map(({id}) => id);
    

    一个例子:

    let userList = [
        { id: "0", name: "ALL" },
        { id: "1", name: "Smith" },
        { id: "2", name: "Smith" },
        { id: "3", name: "RAM" }
    ];
    
    const zeroSymbol = '0';
    const resultArray = userList.filter(f => f.id != zeroSymbol).map(({id}) => id);
    console.log(resultArray)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多