如何在打字稿中将数组推入另一个非空数组?有需要就加入

问题描述 投票:0回答:2

该数组包含至少 10 个对象

 array: any[] = [];

调用函数时,数组应包含“item”中的现有对象和新对象。

 function () {
    var timeline = this.service.fetchservice(10)
    .map((result : Response) => result.json())
    .subscribe(item=> {
          this.array.push(item);
        });
    }

控制台出错

ERROR TypeError: _this.array.push is not a function
arrays angular typescript join push
2个回答
6
投票

类似:

 this.results = this.results.concat(data.results);

-2
投票

使用

function()
时,它采用函数本身的上下文。您应该使用 Typescript 类方法,例如:

export class TestApp {

   someMethod(): void {
      // does something 
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.