jquery怎么循环赋值

snlixing 2018-10-19 04:50:45
.


jquery怎么根据每天最大时长20小时,自动计算每天的产量各是多少呢

每天的自动按小时乘以日产能进行匹配,每天不能超过20小时
...全文
998 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
風灬雲 2018-10-24
  • 打赏
  • 举报
回复
嗯,根据你自己的表格结构找到对应的位置赋值
snlixing 2018-10-24
  • 打赏
  • 举报
回复
引用 9 楼 qq_41114603 的回复:
[quote=引用 7 楼 snlixing 的回复:]



[quote=引用 4 楼 qq_41114603 的回复:]
就是每天的时间超过20就把剩余部分计算到第二天呗,
初始数据应该是

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

*100/100是为了避免小数计算的时候出现的精度问题,所以数据中最多只能有两位小数,或者你有别的办法处理这个问题;
这里的结果格式

res=[
[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:4.7
}],[{
product:"产品2",
time:0.6
}]
]


得到的这个结果怎么赋值到单元格里呢 [/quote]
看你的产品是不是动态的,如果产品是固定的就比较简单了,直接给每一个产品一个唯一标记,用来做当前行的id,然后遍历res,下标index+1就是日期

res.forEach((e,index)=>{
e.forEach(f=>{
$("#“+f.product_id+” td").eq(index).text(f.time)
})
})

如果是动态的,就是说每个表的产品可能不一样;那就多一步处理data,得出产品表的数据;在动态创建表格,然后赋值[/quote]


let res = [];
for(var i = 0, len = data.length; i < len; i++){
test(res, data[i]);
}
console.log(res);
res.forEach((e,index)=>{
e.forEach(f=>{
$("#“+f.product_id+” td").eq(index).text(f.time)
})
})


是不是在这赋值就行?
風灬雲 2018-10-24
  • 打赏
  • 举报
回复
引用 7 楼 snlixing 的回复:
[quote=引用 4 楼 qq_41114603 的回复:]
就是每天的时间超过20就把剩余部分计算到第二天呗,
初始数据应该是

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

*100/100是为了避免小数计算的时候出现的精度问题,所以数据中最多只能有两位小数,或者你有别的办法处理这个问题;
这里的结果格式

res=[
[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:4.7
}],[{
product:"产品2",
time:0.6
}]
]


得到的这个结果怎么赋值到单元格里呢 [/quote]
看你的产品是不是动态的,如果产品是固定的就比较简单了,直接给每一个产品一个唯一标记,用来做当前行的id,然后遍历res,下标index+1就是日期

res.forEach((e,index)=>{
e.forEach(f=>{
$("#“+f.product_id+” td").eq(index).text(f.time)
})
})

如果是动态的,就是说每个表的产品可能不一样;那就多一步处理data,得出产品表的数据;在动态创建表格,然后赋值
snlixing 2018-10-24
  • 打赏
  • 举报
回复
引用 6 楼 qq_41773319 的回复:
[quote=引用 3 楼 snlixing的回复:]怎么才能循环递减呢,就是比如第一款产品用时15.3小时,第四款产品用时5.3小时,超过20小时,那么第四款产品第一天应该可以干20-15.3=4.7小时,第二天可干5.3-4.7=0.6小时,这样依次类推呢

现在就是这个算法写不出来

定义时间一个总常量,再定义一个时间变量,导入时间,用if判断24小时加for循环i就可以实现,[/quote]


思路是这样的,可是不好写,写不出来了
snlixing 2018-10-24
  • 打赏
  • 举报
回复



引用 4 楼 qq_41114603 的回复:
就是每天的时间超过20就把剩余部分计算到第二天呗,
初始数据应该是

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

*100/100是为了避免小数计算的时候出现的精度问题,所以数据中最多只能有两位小数,或者你有别的办法处理这个问题;
这里的结果格式

res=[
[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:4.7
}],[{
product:"产品2",
time:0.6
}]
]


得到的这个结果怎么赋值到单元格里呢
雪糕中的饮料 2018-10-24
  • 打赏
  • 举报
回复
引用 3 楼 snlixing的回复:
怎么才能循环递减呢,就是比如第一款产品用时15.3小时,第四款产品用时5.3小时,超过20小时,那么第四款产品第一天应该可以干20-15.3=4.7小时,第二天可干5.3-4.7=0.6小时,这样依次类推呢

现在就是这个算法写不出来
定义时间一个总常量,再定义一个时间变量,导入时间,用if判断24小时加for循环i就可以实现,
風灬雲 2018-10-24
  • 打赏
  • 举报
回复
兼容方面应该不会有什么问题;这个逻辑只是用了ES6的基础语法和一个forEach遍历;看下是不是赋值或者生成表格的时候有兼容问题
風灬雲 2018-10-24
  • 打赏
  • 举报
回复

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])
}
}
}
snlixing 2018-10-24
  • 打赏
  • 举报
回复
引用 11 楼 qq_41114603 的回复:
嗯,根据你自己的表格结构找到对应的位置赋值



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


这段的思路是什么,有点看不懂
snlixing 2018-10-24
  • 打赏
  • 举报
回复
引用 11 楼 qq_41114603 的回复:
嗯,根据你自己的表格结构找到对应的位置赋值


这个是不是浏览器兼容性不太好,我用360浏览器就不行呢
snlixing 2018-10-24
  • 打赏
  • 举报
回复
谢谢你了,得到的这个结果怎么赋值到单元格里呢
風灬雲 2018-10-23
  • 打赏
  • 举报
回复
就是每天的时间超过20就把剩余部分计算到第二天呗,
初始数据应该是

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

*100/100是为了避免小数计算的时候出现的精度问题,所以数据中最多只能有两位小数,或者你有别的办法处理这个问题;
这里的结果格式

res=[
[{
product:"产品1",
time:15.3
},{
product:"产品2",
time:4.7
}],[{
product:"产品2",
time:0.6
}]
]
snlixing 2018-10-23
  • 打赏
  • 举报
回复
怎么才能循环递减呢,就是比如第一款产品用时15.3小时,第四款产品用时5.3小时,超过20小时,那么第四款产品第一天应该可以干20-15.3=4.7小时,第二天可干5.3-4.7=0.6小时,这样依次类推呢

现在就是这个算法写不出来
snlixing 2018-10-22
  • 打赏
  • 举报
回复
如何解决呢,有好办法吗
snlixing 2018-10-22
  • 打赏
  • 举报
回复
这个不好解决吗

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧