我目前正在尝试对json数据进行一些数学运算。但这不是我想要的。我做了一个循环,使计算深入到每一行(通过我使用angularJS的方式)

这是我尝试处理数据的代码部分:

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


如您所见,我试图从数组中取出一个元素并添加5个元素(这只是一个测试;稍后我想做一些更高级的数学运算)。我可以在console.log中看到它没有按照我的要求做。

例如,如果我的数据是10.25,则我想得到15.25时得到10.255。关于我在做什么错的任何想法吗?

提前致谢

最佳答案

如@epascarello所述,您需要将JSON数据转换为数字。 JSON被序列化为字符串。

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

关于javascript - 对JSON数据进行数学运算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37550950/

10-09 17:01
查看更多