Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免使用Object.assign?_Javascript_Angular_Typescript_Ecmascript 6_Observable - Fatal编程技术网

Javascript 如何避免使用Object.assign?

Javascript 如何避免使用Object.assign?,javascript,angular,typescript,ecmascript-6,observable,Javascript,Angular,Typescript,Ecmascript 6,Observable,我正在运行一个函数来获取类别列表,对于每个类别,获取一个id,然后运行另一个函数来获取特定类别中的“链接”数量,我已经获得了id 为此,我有以下代码: ngOnInit(): void { this.categories = this.categoryService.getCategories(); const example = this.categories.mergeMap((categor) => categor.map((myCateg)

我正在运行一个函数来获取类别列表,对于每个类别,获取一个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():可观察

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

有没有办法让“count”像categoryName一样插入到clientId而不是对象中

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

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

{{
category.categoryName}
{{category.numLinks}
删除

如果我理解正确,您只需执行以下操作:

.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));
}
更改此行:

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


你能以你想要的方式给出一个输出示例吗?什么是“this.category.countLinks(id)”呢?我已经添加了我的模板代码。this.category.countLinks(id)使用getCategories()计算我获得的特定id的链接数。id是控制台中显示的类别的id字段。我应该写入.map(numlinks=>myCateg.numlinks=numlinks.count)吗?这在控制台中不再显示任何内容。这不起作用:{This.categories=This.categorisyservice.getCategories();const示例=This.categories.mergeMap((categor)=>categor.map((myCateg)=>{This.categorisyservice.countcategorilylinks(myCateg.id).map(numlinks=>{myCateg.numlinks=numlinks.count;返回myCateg.id});example.subscribe(val=>console.log(“valeur2:+val”);})刚刚用我的编辑了源代码。它不起作用。我注意到你返回的是
myCateg.id
而不是
myCateg
是,因为我需要获取id来运行计数(id)。只尝试了myCateg。同样的问题。Is.subscribe(valeur=>console.log(valeur));应该在控制台中写入?现在什么都没有显示。这很奇怪。另外,在我的模板中,我显示categories.CategoryName。因此我认为应该在所有操作结束时设置categories变量。您认为控制台中的“未定义”和{{category.numLinks}如何未显示模板中的内容。@PhilippeCorrèges更新了答案。再试一次。@PhilippeCorrèges,这似乎是正确答案,否?这是您遇到的唯一问题:您分配了
numlinks
(具有count属性的对象),但想要
numlinks.count
(仅数字一个)。作为一部分,因为在显示{category.categoryName}的同时,{{category.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));
}
.map(numlinks => Object.assign(myCateg,{numLinks: numlinks}))
.map(numlinks => Object.assign(myCateg, { numLinks: numlinks.count }))