0

(An Angular app)

Im fetching data from https://jobs.github.com/positions while looping their pages, because each page contains 50 available positions, and I want to get the positions from all of their pages.

Service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';


@Injectable({
  providedIn: 'root'
})
export class PositionserviceService {

  // uri = 'https://jobs.github.com/positions'

  constructor(public http: HttpClient) { }

  getAllPositions(){
    return this.http.get('https://cors-anywhere.herokuapp.com/https://jobs.github.com/positions.json');
  }


  getAllPoistions2(page){
    return this.http.get(`https://cors-anywhere.herokuapp.com/https://jobs.github.com/positions.json?page=${page}`);
  }

}

Component.ts:

  getAllPositions(){
    for(let i = 1; i<=5; i++){
      this.positions.push(this.positionService.getAllPoistions2(i))
    }
    console.log("This positions:",this.positions)
    forkJoin(this.positions).subscribe((data:Object[]) => {
      this.allPositions = data
      console.log("All Positions:", this.allPositions)
    })
  }

The response is:

enter image description here

So my question is: how do I merge these 5 arrays into one array?

Much appreciated!

moses
  • 181
  • 2
  • 9
  • 1
    Merging/flattening arrays is already discussed here: https://stackoverflow.com/q/10865025/6513921. It's essentially `this.allPositions = [].concat.apply([], data);` or `this.allPositions = data.flat(1)`. Although `flat()` doesn't support IE. – ruth Jul 30 '20 at 15:00
  • use ``concat()``. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat – azer-p Jul 30 '20 at 15:19

0 Answers0