0

I'm currently trying to do some maths on my json data. But it's not doing what I want. I made a loop so the calculation apples to every row (by the way I work with angularJS)

Here's the part of my code where I'm trying to process the data :

angular.module('recordService', []).factory('recordService', ['$http', function($http) {
  var url;
  var recordService = [];
  recordService.getAll = function(callback) {
    url = "http://localhost/app/www/database/json.php";
    $http({
      url: url
    }).then(function(rs) {
      callback(rs.data);

      function logArrayElements(element, index, array) {
        var thdi = rs.data[index].THDI1_avg;
        console.log(thdi + 5);
      }
      rs.data.forEach(logArrayElements);
    }, function(err) {
      console.log(err)
    })
  }

As you can see I trying to take one element from my array and add 5 to it (it's only a test; I want to do some more advanced math later). I can see in the console.log that's its not doing what I want.

For example, if my data is 10.25, I get 10.255 when I would like 15.25. Any ideas on what I'm doing wrong?

Thanks in advance

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
coco
  • 27
  • 6

2 Answers2

1

You need to convert the JSON data into a number as @epascarello has mentioned. JSON is serialized as strings.

function logArrayElements(element, index, array) {
   var thdi = Number(rs.data[index].THDI1_avg);
   console.log(thdi + 5);
}
Juan
  • 3,675
  • 20
  • 34
  • thanks for your quick answers. Seems kind of obvious when I see it now. But one thing is weird : I get some "NaN" in the console. I figured out that I only get them when my values are not integers. Any idea how to fix that? – coco May 31 '16 at 16:35
  • There is a isNaN() function you can use to drive your logic based on that. – Juan May 31 '16 at 16:55
1

The reason you get 10.255 is, because you are adding 5 to a string.

Try:

console.log(parseFloat(thdi) + 5);

Update regarding Not a Number:

There are a couple of ways you could check whether the value is a number.

isNaN()

if(isNaN(thdi)) {
 console.log("Not a number");
} else {
 console.log("Is a number");
 console.log(parseFloat(thdi) + 5);
}

try / catch

try{
 console.log(parseFloat(thdi) + 5);
} catch(err) {
 console.log("not a number");
}

Edit: Won't give desired result.


typeof

if(typeof thdi === 'number') {
     console.log("Is a number");
     console.log(parseFloat(thdi) + 5);
} else {
console.log("not a number");
}

Also see: How do you check that a number is NaN in JavaScript?

Note: if thdi is undefined, then isNaN() will throw an error. typeof will be able to deal with undefined.

Community
  • 1
  • 1
seN
  • 1,045
  • 14
  • 21
  • Thanks for your answer. As i said to @Juan I get some "NaN" in the console when my number is not an integer.. If you have any idea – coco May 31 '16 at 16:39
  • See my update. Hope that gives you a good idea of different ways to handle it. – seN May 31 '16 at 21:19
  • Thanks for you answer. I figured out that my numbers are defined as NaN because, as I'm french, my numbers displays that way : 1,x. And Js expects a dot. I'll just do a replace in my php file to fix that – coco Jun 01 '16 at 07:08