Search code examples
angularjsangular-resource

lazy loading mapping objects from 2 different server GET requests


Hello i need to query 2 GET requests for 4 -20 dashboards.I was wondering what's a good way to map the second query with the first query?

I am using angularjs $resource, do i have to loop through each object and match them through a unique property? (both id's are the same in both objects) but what if one object is un-ordered while the other is ordered?


Solution

  • For small arrays, the easiest way is to loop through the primary array with angular.foreach and call the $filter service to find the correct matching item in the second array. Pseudo code:

    angular.forEach(primaryArray, function(item, key) {
        item.secondaryItem = $filter('filter')(secondaryArray, {sharedId === item.sharedId})[0]
    }
    

    Also I recommend calling both those REST $resource service queries by getting the promises and joining them back using the $q.all() method.