admin管理员组文章数量:1395185
Requirement is to iterate with a set of values in array.
$scope.weekArray=["Sun","Mon","Tue","Wed","Thurs","Fri","Sat"];
I have a previous duration value and current day. If the previous duration value is 5, I need to find out get the index of the day which is 5 days before the current day. For eg: If current day is Monday, i need to find 5 days before Monday. The answer would be Thursday(including Monday). Please let me know how to do this.
Requirement is to iterate with a set of values in array.
$scope.weekArray=["Sun","Mon","Tue","Wed","Thurs","Fri","Sat"];
I have a previous duration value and current day. If the previous duration value is 5, I need to find out get the index of the day which is 5 days before the current day. For eg: If current day is Monday, i need to find 5 days before Monday. The answer would be Thursday(including Monday). Please let me know how to do this.
Share Improve this question asked Nov 17, 2015 at 14:17 RoopaRoopa 1091 silver badge8 bronze badges 3- Have you gotten anywhere with a solution yourself? It would help to have something to debug :) – Scott Commented Nov 17, 2015 at 14:19
- No,I have not got a solution – Roopa Commented Nov 17, 2015 at 14:21
- @Roopa Did my answer work for you? – Niki van Stein Commented Nov 23, 2015 at 13:55
5 Answers
Reset to default 4A circular iteration over an array in Javascript can be done using the modular %
operator.
Together with array.length
and array.indexOf()
you can do what you want using:
$scope.weekArray=["Sun","Mon","Tue","Wed","Thurs","Fri","Sat"];
var currentDay = "Mon";
var indexOfCurrentDay = $scope.weekArray.indexOf(currentDay);
var whatYouWant = (indexOfCurrentDay-previous_duration);
//note that modulo does not work with negative numbers.
while (whatYouWant < 0)
whatYouWant += $scope.weekArray.length;
whatYouWant = whatYouWant % $scope.weekArray.length;
Here is a working jsFiddle: https://jsfiddle/hf4zqnwu/3/
I can think of two ways. This looks suspiciously like a homework so I'm not going to spell it out for you, apologies if it isn't :P
Modular Arithmetic
JavaScript has a modular operator (%) that is useful for stuff like this.
15 % 7 = 1
21 % 7 = 0
5 % 7 = 5
You can use that to "loop" to the right value. Bear in mind that going back 7 days is the same as going back 21.
The important thing to note is that modular arithmetic won't work for you if you end up with negative numbers, so to avoid issues you may want to add 7 to the number you're subtracting first.
If you do this right, you should be able to handle any duration (even if the duration to go back is 123443434124123 days). Do it wrong, and you'll probably get bugs for any duration over 7 :)
Looping with a counter
Using a for loop or while loop, count back the duration that you need to go back (so from 5 to 0 if you want to go back 5 days). Starting at the start day, subtract 1 from your index each time. When your index drops out of the range of valid indexes in the array, loop back to the end value. i.e., every time you get to 0, go back to 7. The number you get at the end should be your target index.
I'm not going to write the code for you, as I get the feeling this is a school assignment, but why not try the following where x = the number of days previous to today:
- Get the index of the current day.
- While x < 7, x = x - 7.
- If days previous === 0, return current day.
- If index of current day > x return day at current day - x.
- Otherwise, return day at index current day + (7 - x)
just use modulo arithmetic:
(indexOf_currentday - days) % 7 = indexOf_searchedDay
In your example monday has index 1 (1 - 5) % 7 = -4 % 7 = 4 % 7 = 4
//almost same but without array manipulation
var weeksize = 7;
var getDayBeforeIncluding = $scope.getDayBeforeIncluding = function(current, delta){ //current -number of day, delta- days before
var realdelta = delta % weeksize;
var result = current - realdelta;
result = result > 0 ? result : (weeksize + result + 1); //+1 including
return result;
}
var myday = getDayBeforeIncluding (1,5); // 5 days before monday including == 4
console.log(""+myday+"== 4");
//and more smart version both for strings and numbers with argument checking and both including and not including
var weekArray = $scope.weekArray= "Sun","Mon","Tue","Wed","Thurs","Fri","Sat"];
var weeksize = weekArray.length; //no more magic strings
var getDayBefore = $scope.getDayBefore = function(current, delta, including){ //current -number of day, delta- days before
if(null==current){
throw "no day provided";
}
if(typeof(current)=='string'){
current = weekArray.indexOf(current);
}
if(typeof(current)!='number'){
throw "invalid day type";
}
if (current < 0 || current > weeksize) {
throw "invalid current value";
}
var includedelta = including ? 1: 0;
var realdelta = delta % weeksize;
var result = current - realdelta;
result = result > 0 ? result : (weeksize + result + includedelta ); //+1 including
return result;
}
本文标签: javascriptCircular iteration with a set of values in arrayStack Overflow
版权声明:本文标题:javascript - Circular iteration with a set of values in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744762896a2623857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论