当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - compare to object angular js

问题描述:

i have a problem with AngularJS I receive from api json that json contains a prod url and url prerpod I would make calls to these APIs to retrieve a new one json then compare the results to validate which are the same as the problem is that I have a 200 aPI test how can I do. Thank you in advance

ps I think the test objects with the method equals.

i have 100 object like this:

{

"ID": "1",

"URL_preprod": "url1",

"Preprod_bis": "url2",

"prod": "url3",

}

i need to check if the result of call is equals for each object.

 function callAtTimeout() {

if ($scope.preprod && $scope.preprodBis) {

angular.equals($scope.preprod,$scope.preprodBis);

$scope.msg = "equals";

}}

$scope.test = function() {

if (tnrArray) {

for (var i = 0; i < tnrArray.length; i++) {

var urlPreprod = tnrArray[i].URL_preprod;

console.log(urlPreprod);

$http.get(urlPreprod).success( function(response) {

$scope.preprod = response;

console.log(response);

});

var urlPreprodBis = tnrArray[i].Preprod_bis;

console.log(urlPreprodBis);

$http.get(urlPreprodBis).success( function(response) {

$scope.preprodBis = response;

console.log(response);

});

$timeout(callAtTimeout, 3000);

}

网友答案:
var response1;

$http.get("/your/url").then(function(response) {
    response1 = response;
    return $http.get(response.prodUrl);
}).then(function(prodResponse) {
    console.log(prodResponse);
    console.log(_.isEqual(response1 , prodResponse));  // uses lodash
}).catch(function(badResponse) {
    console.log("oops something went wrong", badResponse);
})

this should work - lodash is used to check for equality

TO DO THIS FOR 200 URLs ASYNCHRONOUSLY ...

var urlList = ['/path/url1','/path/url2','/path/url3'];

angular.forEach(urlList, function(url) {

    var response1;

    $http.get(url).then(function(response) {
        response1 = response;
        return $http.get(response.prodUrl);
    }).then(function(prodResponse) {
        console.log(prodResponse);
        console.log(angular.equals(response1 , prodResponse));
    }).catch(function(badResponse) {
        console.log("oops something went wrong", badResponse);
    })
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: