合并两个对象数组与Angular 2和TypeScript? [英] merge two object arrays with Angular 2 and TypeScript?

查看:135
本文介绍了合并两个对象数组与Angular 2和TypeScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经讨论了有关此主题的JavaScript问题,该问题专门涉及带TypeScript的Angular2.

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

我想做的是将json对象连接到一个数组.

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

我的代码看起来像这样

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);
            }
        );
}

如何将data.results连接到this.results并使用打字稿和angular进行连接?

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

this._slugthis._next是在课堂上设置的.

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

谢谢.

推荐答案

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

I think that you should use rather the following:

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

来自concat 文档:

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.

这篇关于合并两个对象数组与Angular 2和TypeScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆