如何在Angular中将ID列表转换为带逗号的字符串对象。

huangapple go评论81阅读模式
英文:

how to get list of ids to string object with comma in angular

问题

我有一个下拉菜单,其中包含以下选项:

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

当选择"ALL"选项时,我需要将所有用户的ID转为字符串对象,不包括0。

string str = {1,2,3}(not include 0)

在我的Typescript代码中:

<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>

我的Typescript代码如下:

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转为字符串对象?

英文:

I have drop down with

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

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

string str = {1,2,3}(not include 0)

 &lt;mat-select placeholder=&quot;User&quot;(ngModelChange)=&quot;modelChanged($event)&quot; formControlName=&quot;user&quot; name=&quot;user&quot;&gt;
  &lt;mat-option *ngFor=&quot;let data of userList&quot; [value]=&quot;data.id&quot;&gt;{{data.name}}  &lt;/mat-option&gt;
  &lt;/mat-select&gt;

my Typescrpt :

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

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

Above this.usertype is any property.

How to get that list of ids into string object?

答案1

得分: 2

你可以使用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)
英文:

You can filter your array and then map just id properties:

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

An example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

const zeroSymbol = &#39;0&#39;;
const resultArray = userList.filter(f =&gt; f.id != zeroSymbol).map(({id}) =&gt; id);
console.log(resultArray)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 19:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定