简体   繁体   中英

Angular forEach with comparison from json?

So I have an input field which fetches data from lets say json1. On submit, I want that input value to be compared to data from json2 and do different stuff depending on the outcome, but I can't wrap my head around it since I can't break the forEach. My code is executing but winds through it all instead of stoping on corresponding if statement.

I've seen a couple of threads talking about using for-loop instead but no luck there either. Any ideas?

I would like something like this:

$scope.superButton = function() {
$http.get(superUrl)
    .then(function(res) {

        angular.forEach(res.data, function(item) {
            // If supertag exists, add to it
            if ($scope.id == item.tag.tag_id) {
                console.log('yay, tag is now supertag');

                $http({
                    method: 'PUT',
                    url: superUrl,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: {
                        'title': $scope.title,
                        'subtitle': $scope.subtitle,
                        'tag': {
                            'title': $scope.selected,
                            'tag_id': $scope.id
                        }
                    }
                }).then(function(data, status, headers, config, statusText) {
                    console.log('added EXISTING supertag:' + data.statusText);

                }).catch(function(err) {
                    console.log(err.data.message);
                });
            }
            // If supertag doesn't exist, create it
            else if ($scope.id != item.tag.tag_id) {
                $http({
                    method: 'POST',
                    url: superUrl,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: {
                        'title': $scope.title,
                        'subtitle': $scope.subtitle,
                        'tag': {
                            'title': $scope.selected,
                            'tag_id': $scope.id
                        }
                    }
                }).then(function(data, status, headers, config, statusText) {
                    console.log('added NEW supertag: ' + data.statusText);

                }).catch(function(err) {
                    console.log(err.data.message);
                });
            }
            // If
            else {
                console.log('no tags');
            }
        });
    });

};

You can use JavaScript Array.prototype.filter() to validate if $http.get() response contains a supertag :

$scope.superButton = function() {
    $http.get(superUrl)
        .then(function(res) {
            var len = res.data.filter(function(item) {
                    return $scope.id === item.tag.tag_id;
                }).length,
                method = (len) ? 'PUT' : 'POST',
                segmentUrl = (len) ? '/' + $scope.id : '',
                msg = (len) ? 'EXISTING supertag: ' : 'NEW supertag: ';

            $http({
                    method: method,
                    url: superUrl + segmentUrl,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: {
                        'title': $scope.title,
                        'subtitle': $scope.subtitle,
                        'tag': {
                            'title': $scope.selected,
                            'tag_id': $scope.id
                        }
                    }
                })
                .then(function(data, status, headers, config, statusText) {
                    console.log(msg + data.statusText);
                })
                .catch(function(err) {
                    console.log(err.data.message);
                });
        });
};

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