Nano Miratus Nano Miratus - 1 year ago 56
jQuery Question

Refactoring if/else statement for time span between two years

So I'm currently programming an web-app, don't want to explain it here, but you can have a look, if you want, at:

Nevermind, the thing is:
Users are able to input two years of a period of time, something like 2005 and 2014 and then the app shows all activity only between these two years, so more specifically the user sees all activity of 2005, 2006, 2007, ... 2014. To make this dynamic and simpler, I just made activity lists of every year (json2005, json2006, ...) and not of every time spans (json2005-2006, json2005-2007) and then found a way to merge all lists between the years the user picked together to a big list of that timespan. The code looks like this:

if (year1 == year2) {
json = window["json" + year1];
else {
if (year2 == (year1 + 1)) {
json = $.extend(window["json" + year1], window["json" + (year1+1)]);
else {
if (year2 == (year1 + 2)) {
json = $.extend(window["json" + year1], window["json" + (year1+1)], window["json" + (year1+2)]);
else {
if (year2 == (year1 + 3)) {
json = $.extend(window["json" + year1], window["json" + (year1+1)], window["json" + (year1+2)], window["json" + (year1+3)]);

is a way to make variables dynamic, if you wonder what that means.)

Code explained:

If year1 is equal to year2, everything is clear, we just take the list of year1 (we could also use year2, because its the same) because we just have one year.

If year2 comes directly after year1 (year2 = year1 + 1), there are no years between them, so we only need two lists, the one of the first year and the one of the second year, basically we merge year1 with year2 and boom, we have a big list with everything from year1 to year2.

If year2 is further away, it all gets a bit ugly. In the code you see, if year2 is year1 + 3, we have to merge together 4(!) different lists and have to write every list down. So we have to write:

json = $.extend(window["json" + year1], window["json" + (year1+1)], window["json" + (year1+2)], window["json" + (year1+3)]);

Buuut, my web app has 25 years to handle, from 1995 to 2020, that makes 325 different merge lines.

My question is:
Is there a way to make this much much shorter than it is now?

Because I think there is a way, I just don't have enough javascript experience and methods knowledge to find it out by myself. I mean, the problem is pretty simple, we just need somehow an index or a way to know the difference between two years to declare how many years we have to merge but here I gave up. :/

Also I had the idea to use factorization, but omg, that's too complicated for my head. But maybe factorization is the keyword, the cue for the solution. I really don't know. Maybe it also could help, if I could write if/else statements into an
; function (can I?).

I know, I know, I'm dumb and all that and probably my english is not the best, but please help me. I really want to make this code shorter.

Thank you! :)

EDIT: You'll get super special thanks in my changelog, because that is really a big problem that needs to be fixed. And you get a cookie. And an upvote. And everything. I would be so so thankful.

Answer Source

How about this?

json = data["json" + year1];
var this_year = year1 + 1;
while(this_year <= year2) {
   json = $.extend(json, data["json" + this_year]);
   this_year = this_year + 1;

Instead of trying to extend everything in one go, it will progressively extend with each year's data. Just be aware that $.extend will replace the value of common keys with the latest one. If you need to merge the values that gets a little harder (but is doable - we would just need an example).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download