87,993
社区成员
发帖
与我相关
我的任务
分享
.
res.forEach((e,index)=>{
e.forEach(f=>{
$("#“+f.product_id+” td").eq(index).text(f.time)
})
})
res.forEach((e,index)=>{
e.forEach(f=>{
$("#“+f.product_id+” td").eq(index).text(f.time)
})
})
function test(res, opt){
let time = 0, index = 0;
if(res.length){//如果res数组里面已经有元素,那么下标取length-1就是最后一个元素
index = res.length - 1
} else {//如果res数组中还没有元素,给他放一个空数组进去
res[index] = []
};
//res[index]找出来的res数组中的最后一个元素进行遍历;计算出这个元素代表的那一天的时间总数time
res[index].forEach(e =>{
time += e.time
})
if(time < 20){//如果这一天的时间总数小于20;说明还能往这个元素里面加
if(time + opt.time > 20){//判断将要加的这个产品的时间加上元素本身有的时间的和是否大于20
res[index].push({
product : opt.product,
time : (20 * 100 - time * 100) / 100, //大于20的情况把这个元素的20个小时补满;
})
opt.time = (opt.time * 100 - (20 * 100 - time * 100)) / 100;//将产品已经加到之前那个元素的时间部分减掉;
test(res, opt);//剩余部分继续执行添加方法;也就是会加到下一个元素中去
} else {//如果要加的这个产品的时间加上元素本身有的时间的和小于20,直接把产品加进去就可以了
res[index].push(opt)
}
} else {//如果这一天的时间总数大于20;说明这个元素已经满了,不能加了,所以要望res里面再追加新的数据,表示下一天的产品安排
if(opt.time > 20){//判断要加的产品事件,如果大于20小时;就直接把20小时放到里面
res.push([{
product : opt.product,
time : 20
}]);
opt.time = (opt.time * 100 - 20 * 100 / 100);//减掉加过的20小时
test(res, opt);//剩余部分继续执行添加方法;也就是会加到下一个元素中去
} else {//如果小于20小时;直接加入最后一个元素
res.push([opt])
}
}
}
data=[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:5.3
}]
let data = [{
product : "产品1",
time : 15.3
},
{
product : "产品2",
time : 5.3
},
{
product : "产品3",
time : 25.3
},
{
product : "产品4",
time : 15.3
}
];
let res = [];
for(var i = 0, len = data.length; i < len; i++){
test(res, data[i])
}
console.log(res);
function test(res, opt){
let time = 0, index = 0;
if(res.length){
index = res.length - 1
} else {
res[index] = []
}
res[index].forEach(e =>{
time += e.time
})
if(time < 20){
if(time + opt.time > 20){
res[index].push({
product : opt.product,
time : (20 * 100 - time * 100) / 100,
})
opt.time = (opt.time * 100 - (20 * 100 - time * 100)) / 100;
test(res, opt)
} else {
res[index].push(opt)
}
} else {
if(opt.time > 20){
res.push([{
product : opt.product,
time : 20
}]);
opt.time = (opt.time * 100 - 20 * 100 / 100);
test(res, opt)
} else {
res.push([opt])
}
}
}
res=[
[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:4.7
}],[{
product:"产品2",
time:0.6
}]
]