Home > Back-end >  Web Api returns multiple lists. How can i differ them and write to different arrays?
Web Api returns multiple lists. How can i differ them and write to different arrays?

Time:12-12

I have web api that returns multiple lists (lets say employersList, locationsList...)

Currently i have this code:

  items = [];

  constructor(private http: HttpClient) {}

  getMember(){
    this.http.get('http://apirequest').toPromise().then(data =>{

      for(let key in data){
        if(data.hasOwnProperty(key))
        {
          this.items.push(data[key])
        }
      }
    })
  }

So i write them all to single array. How can i put the data into separate arrays?

Example of data i get from API request:

{
  "employersList": [
    {
      "id": 2319259,
      "employerName": "Jack Star",
    },
    {
      "id": 4337496,
      "employerName": "John Star",
    }
  ],
  "locationsList": []
}

CodePudding user response:

I think you need something like this

  items = { employersList: [], locationsList: [] };

  constructor(private http: HttpClient) {}

  getMember(){
    this.http.get('http://apirequest').toPromise().then(data =>{
       const { employersList, locationsList } = data;
       this.items.employersList = employersList
       this.items.locationsList = locationsList
    })
  }
  • Related