我正在尝试根据" employeeId"计算总小时数。我无法弄清楚如何为此编写逻辑。解决这个问题的最佳方法是什么?
预期结果,
[
{
"employeeId": "105",
"totalHours": "2:45"
},
{
"employeeId": "777",
"totalHours": "2:15"
}
]
来自Ajax Call的响应
[
{
"employeeId": "105",
"totalHours": "1:30"
},
{
"employeeId": "777",
"totalHours": "1:15"
},
{
"employeeId": "105",
"totalHours": "1:15"
},
{
"employeeId": "777",
"totalHours": "1:00"
}
]
我的代码
var jrr = new Array();
Ext.Ajax.request({
url: '/common/services/general/basicOperations/getDataByModelUsingGetMethod',
method: 'GET',
params : {
actionId : 'payroll',
dataJson : '{"aspectType":"Payroll Profile"}'
},
success: function(response){
try{
var response = response.responseText;
var resObj = Ext.decode(response);
for(var j = 0; j < resObj.data.length; j++)
{
for(var k = 0; k < resObj.data[j].payrolltransactionDetail.length; k++)
{
jrr.push(resObj.data[j].payrolltransactionDetail[k]);
}
}
console.log(JSON.stringify(jrr, null, 4));
}
catch(e){
console.log(e);
}
},
failure: function(response){
deferred.reject("Error Fetching.");
}
});
答案 0 :(得分:1)
这是一个使用js函数式编程的实现。您可以仅使用reduce函数将数据转换为所需的输出。如果您无法在服务器端使用某种聚合更改查询结果,那么您可以实现类似于下面的内容。
var input = [
{
"employeeId": "105",
"totalHours": "1:46"
},
{
"employeeId": "777",
"totalHours": "1:15"
},
{
"employeeId": "105",
"totalHours": "1:15"
},
{
"employeeId": "777",
"totalHours": "1:00"
}
]
var obj = input.reduce( function(init, e){
//if aggregating init object doesn't have the employee then add the employeeId (key) and time (value)
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"];
return init;
//otherwise add to the existing employeeId the hour and minute values, while remembering to carry the extra hour if minutes exceed 59.
}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;
return init;
}
}, {});
var arr = [];
for (var prop in obj) arr.push({employeeId: prop, totalHours: obj[prop].timeString});
console.log(arr);
答案 1 :(得分:1)
这是一个实现。我认为它比其他一些答案更清晰:
function calc(data) {
const seen = {};
const result = [];
data.forEach(item => {
const id = item.employeeId;
if (!seen.hasOwnProperty(id)) {
seen[id] = result.length;
result.push({
employeeId: id,
minutes: 0
});
}
const idx = seen[id];
const parts = item.totalHours.split(':');
result[idx].minutes += (parseInt(parts[0], 10) * 60) + parseInt(parts[1]);
});
result.forEach(item => {
const minutes = item.minutes;
delete item.minutes;
item.totalHours = Ext.String.leftPad(Math.floor(minutes / 60), 2, '0')
+ ':' + Ext.String.leftPad(minutes % 60, 2, '0');
});
return result;
}
console.log(calc([{
"employeeId": "105",
"totalHours": "1:30"
}, {
"employeeId": "777",
"totalHours": "1:15"
}, {
"employeeId": "105",
"totalHours": "1:15"
}, {
"employeeId": "777",
"totalHours": "1:00"
}]));
答案 2 :(得分:0)
添加功能顶部的位置:
var totalHours = 0;
然后内循环是:
for(employee in resObj.data[j].payrolltransactionDetail) {
totalHours += employee.totalHours;
}
然后你可以在外循环之后使用它,但是你认为合适。如果你想对每个事务详细信息求和,请确保在内部循环之前重置为0,并在内部循环之后使用。
答案 3 :(得分:0)
// Expecting input to be your custom object
var output={};
for(i=0;i<input.length;i++){
el = input[i];
a=output[el.id];
a.employeId=el.employeId;
a.totalHours=a.totalHours||"0:0";
b=el.totalHours.split(":");
c=a.totalHours.split(":");
b[0]=+b[0]+c[0];
if(b[1]+c[1]>60){
b[0]++;
}
b[1]=(+b[1]+c[1])%60;
a.totalHours=b.join(":");
}
结果并不完全符合您的预期。我不想每次都在数组中搜索一下该雇员的身份:
{
105:{
employeid:105;
totalHours:"15:20";
}
}