Writing in AngularJS, I've made an $http request to a JSON file within a service and I need to write an if statement to check whether the last value is more or less than the value before. It will only work if I place variables beneath the for loop, and this shouldn't be case.
When I console.log the length or the array it returns 0. I'm not sure whether the issue lies with my array, for loop or if statement. How do I iterate over the objects in the JSON file and push these items into an array?
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"
}
]
}

var last_elandvar prev_el