Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In my controller, I make a rest call to create my table. I need to append some fixed rows and I dont know how to do it.

I want something like this:

export class Environment { id: string; name: string; }

environments: any[]; 
production = new Environment('1', 'production');
development = new Environment('2', 'development');

    ngOnInit(){
            this.environmentService.getEnvironments()
                .subscribe(environments => this.environments = environments,null,() => { this.isLoading = false; });

        } 

So how do I add an array to the promise result?"

  [this.production,this.development] + this.environments;

Results of proposed solution:

 staging: Environment = {
        id: '111',
        name: 'staging'
};

environments = [this.staging];

[ { "id": "111", "name": "staging" }, [ { "id": "86aa96e8-0383-4bce-b833-be3c21f47306", "name": "cloud" } ] ]

The name with cloud is from the server. So close but should look like this:

[ { "id": "111", "name": "staging" },{ "id": "86aa96e8-0383-4bce-b833-be3c21f47306", "name": "cloud" } ]

This worked but is there a better way?:

this.environmentService.getEnvironments()
            .subscribe(environments => {
                for (var i = 0; environments.length > i; i++) 
                { this.environments.push(environments[i])}

            },null,() => { this.isLoading = false; });
share|improve this question
    
Where's the promise result? Where's the array you want to add to? – Günter Zöchbauer Jun 11 '16 at 8:38
    
From the the subscribe. environments => this.environments = environments – Tampa Jun 11 '16 at 8:40
    
This question doesn't really have anything to do with TypeScript, Angular2 or Promises. As such, it should really be edited down to a simple question about how to combine two arrays in javascript. – Tex Jun 11 '16 at 17:28

You're looking for Array.concat():

this.environmentService.getEnvironments()
  .subscribe(env => { this.environments = this.environments.concat(env); },
    null,
    () => { this.isLoading = false; }
  );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.