访问和比较JSON值

时间:2017-05-24 14:13:53

标签: javascript angularjs arrays json

在AngularJS中写道,我对$http内的JSON文件发出service请求,我需要写一个if语句来检查最后一个值是否更多或小于之前的价值。它只有在我将变量置于for循环下方时才有效,而且不应该是这种情况。

console.log长度或数组返回0时。我不确定问题是出在我的arrayfor loop还是if声明中。如何迭代JSON文件中的对象并将这些项目推送到数组中?

app.factory('fxRate', ['$http', '$q', function($http, $q){

    var factory = {};

    factory.getFx = function() {
        return $http.get('../json/mcfx.json');
    }

    return factory;

}]);

app.controller('dashboard', ['$scope', 'fxRate', function($scope, fxRate){

    $scope.dailyFx = function() {
        fxRate.getFx().then(function(response) {
            //handle response or data
            var rates = [];
            var rateData = response.data.fxrate;
            var last_el = rates[rates.length - 1];
            var prev_el = rates[rates.length - 2];            
            console.log(rates)
            for (var i in rateData) {
               rates.push(rateData[i].usdToEur)
            }

            if(last_el > prev_el) {
                console.log('greater'); //0.9786
            } else {
                console.log('lesser'); //0.9232
            }        
        });
    }

    $scope.dailyFx();

}])
{
    "fxrate": [
        {
            "usdToEur": "0.94",
            "date": "23/05/2017"
        },
        {
            "usdToEur": "0.9232",
            "date": "24/05/2017"
        },
        {
            "usdToEur": "0.9786",
            "date": "25/05/2017"
        }       
    ]
}

3 个答案:

答案 0 :(得分:2)

您正在访问rates.length并尝试解决它,就像在您为其分配[]后应该有成员一样,这使得它成为一个空数组。

我想你想要更像这样的东西:

app.controller('dashboard', ['$scope', 'fxRate', function($scope, fxRate){

    $scope.dailyFx = function() {
        var rates = [];
        var rateData = [];
        var last_el
        var prev_el

        fxRate.getFx().then(function(response) {
            //handle response or data

            rateData = response.data.fxrate;
            console.log(rates)
            for (var i in rateData) {
               rates.push(rateData[i].usdToEur)
            }
            last_el = rates[rates.length - 1];
            prev_el = rates[rates.length - 2];            

            if(last_el > prev_el) {
                console.log('greater'); //0.9786
            } else {
                console.log('lesser'); //0.9232
            }        
        });
    }

    $scope.dailyFx();

}])

你基本上是按照错误的顺序正确地做到了。在您的代码中,当这两行执行时,rates数组将始终为空:

var last_el = rates[rates.length - 1];

var prev_el = rates[rates.length - 2];     

答案 1 :(得分:1)

问题是您的then()声明。您在填充last_el数组之前定义prev_elrates。试试这个:

fxRate.getFx().then(function(response) {
  // Extract the USD to EUR rate from the returned response
  var rates = response.data.fxrate.map(function(rate) {
    return rate.usdToEur;
  });
  console.log(rates);

  // Grab the last and previous elements
  var last_el = rates[rates.length - 1];
  var prev_el = rates[rates.length - 2];            

  if(last_el > prev_el) {
      console.log('greater'); //0.9786
  } else {
      console.log('lesser'); //0.9232
  }        
});

您现在应该在控制台中看到一些内容:

{{3}}

答案 2 :(得分:1)

试试这个:

$scope.dailyFx = function() {
    fxRate.getFx().then(function(response) {
        //handle response or data
        var rates = [],
           rateData = response.data.fxrate,
           last_el, prev_el;

        for (var i in rateData) {
           rates.push(rateData[i].usdToEur)
        }

        if (rateData.length > 1) {
            last_el = rates[rateData.length - 1];
            prev_el = rates[rateData.length - 2];            
        }

        if (last_el > prev_el) {
            console.log('greater'); //0.9786
        } else {
            console.log('lesser'); //0.9232
        }        
    });
}