mtorn mtorn - 4 months ago 20
JSON Question

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');
}
});
});


};

Answer

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 arr = res.data.filter(function(item) {
                    return $scope.id === item.tag.tag_id;
                }),
                method = (arr.length) ? 'PUT' : 'POST',
                segmentUrl = (arr.length) ? '/' + $scope.id : '',
                msg = (arr.length) ? '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);
                });
        });
};