-1

i am try to build Angular project , i have tow different API news sources . l want to display the tow API news as randomly like :

  • element news 1
  • element news 2
  • element news 1
  • element news 2

But what i have in my code below is

  • element news 1 loop
  • element news 2 loop

news.ts

 news_one_array:any
 news_tow_array:any

ngOnInit() {

this.news_one()
this.news_tow()
}


async news_one() {
    this.http.get("/nodes/25/iq/0/20/xlarge")
      .pipe(timeout(5000),
        catchError(err => {
          console.log(err)
          return Observable.throw("Timeout has occurred");
        })).subscribe(data => {

          this.news_one_array = data
        })

  }

  async news_tow() {
    this.http.get("/nodes/25/iq/0/21/xlarge")
      .pipe(timeout(5000),
        catchError(err => {
          console.log(err)
          return Observable.throw("Timeout has occurred");
        })).subscribe(data => {

          this.news_tow_array = data
        })

  }

HTML :

// *ngfor news 1 

<div *ngFor="let items of news_one_array; let i=index">
<p>{{items.title}}</p>
</div>

// *ngfor news 2 
<div *ngFor="let items of news_tow_array; let i=index">
<p>{{items.title}}</p>
</div>

output :

enter image description here

Ali Ghassan
  • 1,280
  • 1
  • 22
  • 59

2 Answers2

0

Have only one ngFor loop in your template on new array formed below :-

make a new array of both news like :-

public newArray = [];

    async news_one() {
        this.http.get("/nodes/25/iq/0/20/xlarge")
          .pipe(timeout(5000),
            catchError(err => {
              console.log(err)
              return Observable.throw("Timeout has occurred");
            })).subscribe(data => {

              this.news_one_array = data
              this.newsArray = [...this.news_one_arraynewsArray, ...data];
              //shuffle array here
            })

      }

      async news_tow() {
        this.http.get("/nodes/25/iq/0/21/xlarge")
          .pipe(timeout(5000),
            catchError(err => {
              console.log(err)
              return Observable.throw("Timeout has occurred");
            })).subscribe(data => {

              this.news_tow_array = data
              this.newsArray = [...this.news_one_array];newsArray, ...data];
              //shuffle array here
            })

      }

then shuffle this array. To know how to shuffle a array. find method on link below :-

How to randomize (shuffle) a JavaScript array?

Picked up shuffle function from above link :-

public shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

Should be used like

this.newsArray = this.shuffle(this.newsArray);
Aakash Garg
  • 10,649
  • 2
  • 7
  • 25
0

You can use map function to combine two arrays into a single array of objects as follows.

    listA = [1, 2, 3 , 4];
    listB = ['a', 'b', 'c']
    this.combined = this.listA.map((x, i)=> {
          return {"a": x, "b": this.listB[i]}
    } );

And then use it in the html. For uneven arrays, use *ngIf to check null/undefined.

<div *ngFor="let item of combined">  
    {{item.a}}
    {{item.b}}
</div>
Krishna Mohan
  • 1,612
  • 3
  • 19
  • 27