如何避免使用Object.assign?(How to avoid using Object.assign?)

编程入门 行业动态 更新时间:2024-10-22 23:12:47
如何避免使用Object.assign?(How to avoid using Object.assign?)

我正在运行一个函数来获取类别列表,并为每个类别获取一个id并运行另一个函数来获取该特定类别中我已获得id的“链接”数量。

为此,我有以下代码:

ngOnInit(): void { this.categories = this.categoryService.getCategories(); const example = this.categories.mergeMap((categor) => categor.map((myCateg) => { this.categoryService.countCategoryLinks(myCateg.id) .map(numlinks => Object.assign(myCateg,{numLinks: numlinks})) .subscribe(valeur => console.log(valeur)); return myCateg.id })); example.subscribe(val => console.log("valeur2: "+val)); }

getCategories()的位置是:

getCategories(): Observable<any> { return this.category.find({where: {clientId: this.userApi.getCurrentId()}}) };

和countCategoryLinks()是:

countCategoryLinks(id: number): Observable<any> { return this.category.countLinks(id) };

看来,如下面的屏幕截图所示:

numlinks是一个Object。 当然这是由Object.assign完成的。

有没有办法像categoryName ot clientId而不是Object一样插入“count”?

我的目标是能够在模板中显示所有值

<tr *ngFor="let category of categories | async; let id = index"> <td> <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row[3]"> <input type="checkbox" id="row[3]" class="mdl-checkbox__input"/> </label> </td> <td class="mdl-data-table__cell--non-numeric"><a [routerLink]="['/category/links']"></a>{{ category.categoryName }} </td> <td class="mdl-data-table__cell--non-numeric">{{category.numLinks}}</td> <td #category.id><i (click)="deleteCategory(id)" class="material-icons">delete</i></td> </tr>

I am running a function to get a list of categories and for each category, get an id and run another function to get the number of 'links' in that particular category I have got the id.

For that, I have the following code:

ngOnInit(): void { this.categories = this.categoryService.getCategories(); const example = this.categories.mergeMap((categor) => categor.map((myCateg) => { this.categoryService.countCategoryLinks(myCateg.id) .map(numlinks => Object.assign(myCateg,{numLinks: numlinks})) .subscribe(valeur => console.log(valeur)); return myCateg.id })); example.subscribe(val => console.log("valeur2: "+val)); }

where getCategories() is:

getCategories(): Observable<any> { return this.category.find({where: {clientId: this.userApi.getCurrentId()}}) };

and countCategoryLinks() is:

countCategoryLinks(id: number): Observable<any> { return this.category.countLinks(id) };

It appears, as shown in the screenshot below:

that numlinks is an Object. Of course this is done by Object.assign.

Is there a way to have the "count" inserted like categoryName ot clientId instead of an Object?

My goal is to be able to show all the values in a template:

<tr *ngFor="let category of categories | async; let id = index"> <td> <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row[3]"> <input type="checkbox" id="row[3]" class="mdl-checkbox__input"/> </label> </td> <td class="mdl-data-table__cell--non-numeric"><a [routerLink]="['/category/links']"></a>{{ category.categoryName }} </td> <td class="mdl-data-table__cell--non-numeric">{{category.numLinks}}</td> <td #category.id><i (click)="deleteCategory(id)" class="material-icons">delete</i></td> </tr>

最满意答案

如果我理解正确,那么你可以简单地这样做:

.map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; })

代替

.map(numlinks => Object.assign(myCateg,{numLinks: numlinks}))

因此,您的代码应该是这样的:

{ this.categories = this.categoryService.getCategories(); const example = this.categories .mergeMap((categor) => categor .map((myCateg) => { this.categoryService .countCategoryLinks(myCateg.id) .map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; }) .subscribe(valeur => console.log(valeur)); return myCateg.id; })); example.subscribe(val => console.log("valeur2: " + val)); }

If I understand this correctly, then you can simply do this:

.map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; })

Instead of

.map(numlinks => Object.assign(myCateg,{numLinks: numlinks}))

Therefore your code should become something like this:

{ this.categories = this.categoryService.getCategories(); const example = this.categories .mergeMap((categor) => categor .map((myCateg) => { this.categoryService .countCategoryLinks(myCateg.id) .map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; }) .subscribe(valeur => console.log(valeur)); return myCateg.id; })); example.subscribe(val => console.log("valeur2: " + val)); }

更多推荐

本文发布于:2023-08-07 09:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Object   assign   avoid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!