Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70745

The best way to merge two arrays in one arrays and filter this

$
0
0

@filippodicostanzo wrote:

I need a suggestion to merge two arrays that get data from promise and filter this merged array.

I try but i have a trouble with async data.

public sponsored_items: any = [];

constructor() {
this.get_structures();
this.get_banners();
this.arrayFilter();
}

  get_structures() {
    this.getStrucutres.get_structures(1)
      .then(data => {
        console.log(data);
        this.partners = data.results
        this.arraypartners = this.partners

        for (var i = 0; i < this.partners.length; i++) {
          if (this.partners[i].sponsored == "1") {
            this.sponsored_items.push(this.partners[i]);
          }
        }
      });
  }


  get_banners() {
    this.getBanners.get_banners(1)
      .then(data => {
        console.log(data);
        this.banners = data.results

        for (var i = 0; i < this.banners.length; i++) {
          this.banners[i].banner = true;
          this.sponsored_items.push(this.banners[i]);
        }
      });
  }

  arrayFilter() {
    console.log(this.sponsored_items);
    this.shuffleArray(this.sponsored_events);
    this.shuffleArray(this.sponsored_items);
    this.sponsored_events = this.sponsored_events.filter((item, index) => index < 5)
    this.sponsored_items = this.sponsored_items.filter((item, index) => index < 1);
    console.log(this.sponsored_items);

  }

  shuffleArray(array) {
    array.sort(function (a, b) {
      return (0.5 - Math.random());
    });
  }

The function arrayFilter is executed but I need that this.sponsored_items is filled

This is a problem with async data can I resolve with a good pattern?

Thank You!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>