我不是百分百肯定如何说出这个问题,所以请随意将问题标题更改为有意义的内容。
我有一个对象solution
,其中包含一个属性名days
,其中包含10个数组,请参阅下面的示例
{
"sameShiftHolds": true,
"sameStaffHolds": true,
"sameRoomHolds": true,
"days": [{
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": false
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": false
}, {
"availableStaff": [
[0, 1, 4, 3, 5, 9, 22, 44],
[0, 1, 4, 3, 5, 9, 22, 44],
[4, 8, 7]
],
"availableRooms": [
[3, 6, 77, 89, 23],
[3, 6, 77, 89, 23],
[2, 7, 9]
],
"suitableStaff": [
[22, 44],
[22, 44],
[4]
],
"suitableRooms": [
[89, 23],
[22, 44],
[2]
],
"ValidStartDate": true
}]
}
在我的网站上,我有一个星期的自定义日历视图,该视图是从模板创建的,从星期一到星期五开始。默认情况下,有2周但用户可以更改他们想要查看的周数。每个星期都会被一个具有唯一ID "solCol0"
,"solCol1"
等等的div拆分。
然后我循环选择的周数,在这种情况下我们有2
。
然后循环遍历solution
的长度,在这种情况下为10
。
我只想循环5次(对于每周的每一天),然后在5次循环之后,将columdId递增1以将细节附加到下一周,例如将前5个循环附加到"solCol0"
然后接下来的5个被附加到“solCol1
”,如果用户选择了超过2周,到3,solution
长度将增加到15,所以接下来的5个循环将附加到“'solCol2”等...
很抱歉,如果这不是很清楚,通常我只需要在每5次循环后增加一个值。任何帮助将不胜感激。
loadSolutionStartRows: function(dates, solution) {
var self = this;
for (var i = 0; i < dates.length; i++) {
var columnId = "#solCol" + i;
var startDate = moment(dates[i], 'Do MMM');
var rowDates = [];
var iterate = 5;
for (var d = 0; d < solution.days.length; d++) {
//Every 5 loops - columnId = "#solCol" + i + 1;
rowDates.push(moment(startDate).format('ddd (Do MMM)'));
startDate.add(1, 'days');
var selectedDate = rowDates[d];
var statusClass;
var statusIconClass;
if (solution.days[d].ValidStartDate === true) {
statusClass = "sxpTableHeaderIconGreenStatus";
statusIconClass = "octicon " + "octicon-check";
}
if (solution.days[d].ValidStartDate === false) {
statusClass = "sxpTableHeaderIconRedStatus";
statusIconClass = "octicon " + "octicon-x";
}
$(columnId).append(self.solutionTableRow({
rowId: i + 1,
date: selectedDate,
statusClass: statusClass,
statusIconClass: statusIconClass,
trainerCountEarly: 1,
trainerListEarly: 1,
roomCountEarly: 1,
roomListEarly: 1,
trainerCountLate: 1,
trainerListLate: 1,
roomCountLate: 1,
roomListLate: 1
}));
}
}
}
},
使用上面的代码我实现了下面的图像,它增加了10天而不是我想要的5天。
答案 0 :(得分:4)
你会想要一个每5次迭代递增一次的变量,所以你需要检查索引是否是5的倍数:
var foo = 0;
for (var i; i < someLength; ++i) {
if (i % 5 === 0) {
foo++;
}
}
这使用模运算符来获取i / 5
的余数。如果它是0,那么我们知道索引是5的倍数。
请注意,这绝对是不必要的,也可以只做
Math.floor(someLength / 5);
或者更简洁:
someLength / 5 | 0;
两者都会将除法的结果截断为整数,并且您将知道有多少5 someLength
包含。
答案 1 :(得分:1)
您可以使用Math.floor(i/5)
。它只是将i
除以5,然后将其舍入。
对于i的值,从0到4,这将返回1,对于i的值从5到9等,将返回1.
在你的例子中:
var columnId = "#solCol" + Math.floor(i/5);
答案 2 :(得分:0)
对于循环的每一步,检查迭代器是否可被5 if (i % 5 == 0)
整除。如果是,请增加columnId变量
答案 3 :(得分:0)
就像我评论的那样,你需要一个超出for循环范围的变量,当内循环变量是5的倍数时你可以增加。这是一个例子。
var outerVar = 0;
for (var innerVar = 1; innerVar <= 10; innerVar++) {
console.log("Iteration " + innerVar + " uses outerVar " + outerVar + ".");
if (innerVar % 5 === 0) {
outerVar++;
}
}
&#13;