How to join two arrays at the angle

Asked

Viewed 234 times

0

I have a service me returning an array containing photos and when I "scroll" page it adds +1 on the photo page and I wanted to show the new photos below the current photos

  getPhotos(page: number) {
    this.updateMasonryLayout = true;
    this.unsplashAPI.random(0 + page)
      .subscribe(
        (photos: any[]) => this.photos = photos,
        (error: any) => this.errorMessage = error as any
      );
  }

This code returns me the page but always changes the current page or exchange the values of the array and I want to keep the old values plus new values

2 answers

3


With the es6 has the possibility to do with deconstructing

arrNovo = [...arr1, ...arr2] // voce pode fazer com quantos arrays quiser e a ordem importa.
  • i would have to create an array to receive the old one first? pq I wanted to keep adding the return in the current array as one +=

  • you can do arrAtual= [...arrAtual, ...arrNovo]

  • so it gets this.photos =[...this.photos, ...photos]?

0

You can do it this way:

getPhotos(page: number) {
    this.updateMasonryLayout = true;
    this.unsplashAPI.random(0 + page)
      .subscribe(
        (photos: any[]) => this.photos = this.photos.concat(photos),
        (error: any) => this.errorMessage = error as any
      );
  }
  • did not work this way presents the following error Cannot read Property 'Concat' of Undefined

  • The this.photos should be started with an empty array. Probably it is not being started, with this coming undefined and bursting the error. .concat is an array method.

  • it is initialized as array

  • There are ways you can put the code of the Component and the service to have a better idea of what might be happening then?

  • There are as many things before that but I researched about the Concat and it no longer works from angular 5

  • Do you have to provide the link from where you got this information? Because I’ve used the .concat in an angular design 7.

  • https://stackoverflow.com/questions/38092458/merge-two-object-arrays-with-angular-2-and-typescript/46712822

  • 1

    Concat works on any version of angular. Everything you can do with vanilla js you can do with angular

  • so I must have declared this.photos wrong but I don’t see where it’s wrong, thanks for the help

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.