如何在打字稿中将数据推送到数组中

问题描述 投票:-1回答:1

我想通过从两个集合(supcat和cat)中获取数据,然后将两者组合以创建一个新数组,从而在菜单页面加载时访问该数组,但push()无法正常工作,从而创建动态菜单栏。

ngOnInit() {
this.cattest();}


cattest(){
var x;
this.supcatobj.fetchsupcat().subscribe(
  (res)=>
  {
    if(res.length!=0)
    {this.supcat=res;
      for(let x=0;x<this.supcat.length; x++)
      {
        this.catobj.fetchallcat(this.supcat[x]["_id"]).subscribe(
          (resp)=>
          {
            this.allcat=resp;

            for(let y=0;y<this.allcat.length;y++)
            {



            }
            this.testarr[x].push(this.supcat[x]["supcatname"],this.allcat);

          }
        );
      }
    }
  }
);}
node.js angular mongodb typescript array-push
1个回答
0
投票

代替嵌套的subscription()调用,我将尝试为两个不同的集合编写单独的可观察对象,然后使用CombineLatest()运算符将它们组合到所需的数组中。很难确切地看到您的工作目标,但是从概念上讲,它将是这样的:

© www.soinside.com 2019 - 2024. All rights reserved.