简体   繁体   中英

Push object to empty array after condition is met

I've created a couple of services that grab data from a REST API. These services return objects with names, ids, and some unique keys pertaining to a foo or a bar name. I also have a service doing the same for businesses, also with names, ids, and what foo / bar is tied to that business.

Unfortunately, the data model for this is...not ideal. Rather than just showing which foo / bar is attached to that business, it has every single foo or bar for every single business with a published: true/false key/val pair.

What I'm attempting to do is grab the URL name, loop through my foo object, check to see if the name from the current URL and the data match, and if they do store that object in $scope.results . From here, I want to loop through my businesses object and check to see if its conditionData id matches that of the new $scope.results array's id. Once this condition is met, I want to store those businesses in a $scope.businesses array. As it stands right now, I'm getting all businesses returned, rather than just the ones that have the same id as the current $scope.results id. I suspect the issue is either a) I'm a noob (most likely) or b) the published: true/false is creating issues.

Thanks in advance for any help, let me know if I need to clarify anything else. I'm still pretty new to Angular and JS as a whole, so I'm not sure if how I'm attempting to do this is super optimal. I'm open to better ideas if anyone has any.

.controller('ResultsController', function($scope, $location, getData) { 
    $scope.businesses = [];
    $scope.results = [];

    var url = $location.path().split('/')[2];    // we do this because it's always going to follow a pattern of /:base/:name

    function init() {

        getData.getConditions().success(function(data) {
            var tempCondition = data;
            var tempData;

            for (var condition in tempCondition) {
                tempData = tempCondition[condition];
                if (url === tempData.name) {
                    $scope.results = tempData;
                }
            }
        })  
        .error(function(data, status, headers, config) {
            console.log('err: ' + data);
        });

        getData.getBusinesses().success(function(data) {
            var tempBusinesses = data,
                tempConditionData;

            for (var business in tempBusinesses) {
                tempConditionData = tempBusinesses[business].conditionData;

                for (var condition in tempConditionData) {
                    if (tempConditionData[condition].id === $scope.results.id) {
                        $scope.businesses.push(tempBusinesses[business]);
                    }   
                }
            }
        })
        .error(function(data, status, headers, config) {
            console.log('err: ' + data);
        });

    }

    init();
});

The two http calls you are using may also be problematic as they depend one each other. what if the first calls takes some time, your second http call returns first.

I find myself using SO as a rubber duck most of the time, I figured it out basically as soon as I finished typing the question. It was due to the published: true/false key/val pair.

All I had to do was change

for (var condition in tempConditionData) {
    if (tempConditionData[condition].id === $scope.results.id) {
        $scope.businesses.push(tempBusinesses[business]);
    }   
}

to

for (var condition in tempConditionData) {
    if (tempConditionData[condition].id === $scope.results.id && tempConditionData[condition].published === true ) {
        $scope.businesses.push(tempBusinesses[business]);
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM