无法计算总小时数

时间:2016-11-29 05:38:07

标签: javascript

我是JavaScript新手。我们有一个代码可以根据"员工ID"添加总小时数。下面的代码显示" excessTime"的正确结果。但是对于" totalHours"它正在显示" NaN"。有谁知道为什么它会显示" NaN"同时添加" totalHours"以及如何解决它?

<!DOCTYPE html>
<html>
<body>
<script>
var input = [
    {
        "employeeId": "101",
        "totalHours": "1:46",
        "excessTime": "2:15"
    },
    {
        "employeeId": "102",
        "totalHours": "1:15",
        "excessTime": "1:30"
    },
    {
        "employeeId": "101",
        "totalHours": "1:15",
        "excessTime": "3:00"
    },
    {
        "employeeId": "102",
        "totalHours": "1:00",
        "excessTime": "2:15"
    }
]

    var obj = input.reduce( function(init, e){
    if (init[e["employeeId"]] == undefined){
         init[e["employeeId"]] = {hours: parseInt(e["totalHours"].split(":")[0]),minutes: parseInt(e["totalHours"].split(":")[1])};
         init[e["employeeId"]].timeString = e["totalHours"];
         init[e["employeeId"]] = {hrs: parseInt(e["excessTime"].split(":")[0]),mns: parseInt(e["excessTime"].split(":")[1])};
         init[e["employeeId"]].excessString = e["excessTime"];
         return init;
      } else {
         init[e["employeeId"]].hours += (parseInt(e["totalHours"].split(":")[0]) + Math.floor((init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))/60));
         init[e["employeeId"]].minutes = (init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))%60;
         init[e["employeeId"]].timeString = init[e["employeeId"]].minutes > 9 ? init[e["employeeId"]].hours + ":" + init[e["employeeId"]].minutes : init[e["employeeId"]].hours + ":0" + init[e["employeeId"]].minutes;

         init[e["employeeId"]].hrs += (parseInt(e["excessTime"].split(":")[0]) + Math.floor((init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))/60));
         init[e["employeeId"]].mns = (init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))%60;
         init[e["employeeId"]].excessString = init[e["employeeId"]].mns > 9 ? init[e["employeeId"]].hrs + ":" + init[e["employeeId"]].mns : init[e["employeeId"]].hrs + ":0" + init[e["employeeId"]].mns;
         return init;
      }
  }, {});

var arr = [];
for (var prop in obj) arr.push({employeeId: prop, totalHours: obj[prop].timeString, excessTime: obj[prop].excessString});
console.log(arr);
</script>
</body>
</html>

输出:

[
    {
        "employeeId": "101",
        "totalHours": "NaN:0NaN",
        "excessTime": "5:15"
    },
    {
        "employeeId": "102",
        "totalHours": "NaN:0NaN",
        "excessTime": "3:45"
    }
]

3 个答案:

答案 0 :(得分:2)

您正在尝试为hoursminutes属性分配值,但它们是undefined。您需要在第一个if声明中分别将它们分配到小时和分钟:

init[e["employeeId"]].hours = parseInt(e["totalHours"].split(":")[0]);
init[e["employeeId"]].minutes = parseInt(e["totalHours"].split(":")[1]);

一起看起来像这样:

var input = [
    {
        "employeeId": "101",
        "totalHours": "1:46",
        "excessTime": "2:15"
    },
    {
        "employeeId": "102",
        "totalHours": "1:15",
        "excessTime": "1:30"
    },
    {
        "employeeId": "101",
        "totalHours": "1:15",
        "excessTime": "3:00"
    },
    {
        "employeeId": "102",
        "totalHours": "1:00",
        "excessTime": "2:15"
    }
]

    var obj = input.reduce( function(init, e){
    if (init[e["employeeId"]] == undefined){
         init[e["employeeId"]] = {hours: parseInt(e["totalHours"].split(":")[0]),minutes: parseInt(e["totalHours"].split(":")[1])};
         init[e["employeeId"]].timeString = e["totalHours"];
         init[e["employeeId"]] = {hrs: parseInt(e["excessTime"].split(":")[0]),mns: parseInt(e["excessTime"].split(":")[1])};
         init[e["employeeId"]].excessString = e["excessTime"];
         init[e["employeeId"]].hours = parseInt(e["totalHours"].split(":")[0]);
         init[e["employeeId"]].minutes = parseInt(e["totalHours"].split(":")[1]);
         return init;
      } else {
         init[e["employeeId"]].hours += (parseInt(e["totalHours"].split(":")[0]) + Math.floor((init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))/60));
         init[e["employeeId"]].minutes = (init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))%60;
         init[e["employeeId"]].timeString = init[e["employeeId"]].minutes > 9 ? init[e["employeeId"]].hours + ":" + init[e["employeeId"]].minutes : init[e["employeeId"]].hours + ":0" + init[e["employeeId"]].minutes;

         init[e["employeeId"]].hrs += (parseInt(e["excessTime"].split(":")[0]) + Math.floor((init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))/60));
         init[e["employeeId"]].mns = (init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))%60;
         init[e["employeeId"]].excessString = init[e["employeeId"]].mns > 9 ? init[e["employeeId"]].hrs + ":" + init[e["employeeId"]].mns : init[e["employeeId"]].hrs + ":0" + init[e["employeeId"]].mns;
         return init;
      }
  }, {});

var arr = [];
for (var prop in obj) arr.push({employeeId: prop, totalHours: obj[prop].timeString, excessTime: obj[prop].excessString});
console.log(arr);

<强>结果:

  [{
        "employeeId": "101",
        "totalHours": "3:01",
        "excessTime": "5:15"
    }, {
        "employeeId": "102",
        "totalHours": "2:15",
        "excessTime": "3:45"
    }]

JSFiddle: https://jsfiddle.net/v7eckcny/

答案 1 :(得分:1)

因为是String,使用parseInt()将字符串转换为整数 像这个例子:

var a = parseInt("10");

答案 2 :(得分:0)

在您的reduce if子句中,您正在初始化init[e["employeeId"]]两次 ,所以当你第二次初始化你的{hours , minutes}对象被{hrs,mns}取代时,这就是为什么你总共得到NAN的原因。

你需要初始化一次

init[e["employeeId"]] = {hours: parseInt(e["totalHours"].split(":")[0]),minutes: parseInt(e["totalHours"].split(":")[1]),hrs: parseInt(e["excessTime"].split(":")[0]),mns: parseInt(e["excessTime"].split(":")[1])};

查看下面的工作代码段。

&#13;
&#13;
var input = [
    {
        "employeeId": "101",
        "totalHours": "1:46",
        "excessTime": "2:15"
    },
    {
        "employeeId": "102",
        "totalHours": "1:15",
        "excessTime": "1:30"
    },
    {
        "employeeId": "101",
        "totalHours": "1:15",
        "excessTime": "3:00"
    },
    {
        "employeeId": "102",
        "totalHours": "1:00",
        "excessTime": "2:15"
    }
];

var obj = input.reduce( function(init, e){
    if (init[e["employeeId"]] == undefined){
      // here you need to change your code like below
         init[e["employeeId"]] = {hours: parseInt(e["totalHours"].split(":")[0]),minutes: parseInt(e["totalHours"].split(":")[1]),hrs: parseInt(e["excessTime"].split(":")[0]),mns: parseInt(e["excessTime"].split(":")[1])};
         init[e["employeeId"]].timeString = e["totalHours"];
         init[e["employeeId"]].excessString = e["excessTime"];
         return init;
      } else {
         init[e["employeeId"]].hours += (parseInt(e["totalHours"].split(":")[0]) + Math.floor((init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))/60));
         init[e["employeeId"]].minutes = (init[e["employeeId"]].minutes + parseInt(e["totalHours"].split(":")[1]))%60;
         init[e["employeeId"]].timeString = init[e["employeeId"]].minutes > 9 ? init[e["employeeId"]].hours + ":" + init[e["employeeId"]].minutes : init[e["employeeId"]].hours + ":0" + init[e["employeeId"]].minutes;

         init[e["employeeId"]].hrs += (parseInt(e["excessTime"].split(":")[0]) + Math.floor((init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))/60));
         init[e["employeeId"]].mns = (init[e["employeeId"]].mns + parseInt(e["excessTime"].split(":")[1]))%60;
         init[e["employeeId"]].excessString = init[e["employeeId"]].mns > 9 ? init[e["employeeId"]].hrs + ":" + init[e["employeeId"]].mns : init[e["employeeId"]].hrs + ":0" + init[e["employeeId"]].mns;
      
         return init;
      }
  }, {});

var arr = [];
for (var prop in obj) arr.push({employeeId: prop, totalHours: obj[prop].timeString, excessTime: obj[prop].excessString});
console.log(arr);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;