合并两个对象数组与角2和打字稿?

时间:2022-10-04 12:13:16

I have gone across the JavaScript questions on this topic, this question is specifically about Angular2 with TypeScript.

我已经讨论了关于这个主题的JavaScript问题,这个问题是关于带有打字稿的Angular2的。

What I am trying to do is to concatenate the json objects to an array.

我要做的是将json对象连接到一个数组。

My code looks something like this,

我的代码是这样的,

public results: [];


public getResults(){
    this._service.get_search_results(this._slug, this._next).subscribe(
            data => {
                this.results.concat(data.results);
                this._next = data.next;
            },
            err => {
                console.log(err);
            }
        );
}

How can I concatenate data.results to this.results with typescript and angular?

如何连接数据。这个结果。结果是打字稿和角?

this._slug and this._next are set on class.

这一点。_slug这。_next在类上设置。

thanks.

谢谢。

4 个解决方案

#1


59  

I think that you should use rather the following:

我认为你应该使用以下内容:

data => {
  this.results = this.results.concat(data.results);
  this._next = data.next;
},

From the concat doc:

concat的医生:

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

concat()方法返回一个新的数组,该数组由它所调用的数组与作为参数的数组和/或值组成。

#2


38  

The spread operator is kinda cool.

扩展操作符很酷。

this.results = [ ...this.results, ...data.results];

The spread operator allows you to easily place an expanded version of an array into another array.

扩展操作符允许您轻松地将一个数组的扩展版本放置到另一个数组中。

You can read about spread operator here.

你可以在这里读到传播算子。

#3


1  

You can also use the form recommended by ES6:

你也可以使用ES6推荐的表格:

data => {
  this.results = [
    ...this.results,
    data.results,
  ];
  this._next = data.next;
},

This works if you initialize your array first (public results = [];); otherwise replace ...this.results, by ...this.results ? this.results : [],.

如果首先初始化数组(public results = [];);否则取代……这。结果,……这。结果吗?这一点。结果:[]。

Hope this helps

希望这有助于

#4


0  

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

https://www.w3schools.com/jsref/jsref_concat_array.asp

https://www.w3schools.com/jsref/jsref_concat_array.asp

#1


59  

I think that you should use rather the following:

我认为你应该使用以下内容:

data => {
  this.results = this.results.concat(data.results);
  this._next = data.next;
},

From the concat doc:

concat的医生:

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

concat()方法返回一个新的数组,该数组由它所调用的数组与作为参数的数组和/或值组成。

#2


38  

The spread operator is kinda cool.

扩展操作符很酷。

this.results = [ ...this.results, ...data.results];

The spread operator allows you to easily place an expanded version of an array into another array.

扩展操作符允许您轻松地将一个数组的扩展版本放置到另一个数组中。

You can read about spread operator here.

你可以在这里读到传播算子。

#3


1  

You can also use the form recommended by ES6:

你也可以使用ES6推荐的表格:

data => {
  this.results = [
    ...this.results,
    data.results,
  ];
  this._next = data.next;
},

This works if you initialize your array first (public results = [];); otherwise replace ...this.results, by ...this.results ? this.results : [],.

如果首先初始化数组(public results = [];);否则取代……这。结果,……这。结果吗?这一点。结果:[]。

Hope this helps

希望这有助于

#4


0  

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

https://www.w3schools.com/jsref/jsref_concat_array.asp

https://www.w3schools.com/jsref/jsref_concat_array.asp