Crazy nested for loops (i.e. a Beautiful Mind)

I was working on a project today, that dealt with a large amount of JSON data.  The project needed to allow the user to filter the data by a bunch of different filters and options within those filters.

Initially I started using Underscore.js thinking this would help, but in the interest of time (had to get it done today!!!!) I decided to just do it quick and dirty.  Well, I succeeded in the dirty department:

if (filters.length > 0) {
    for (var t = 0; t < d.resources[i].tags.length; t++) {
        for (var f = 1; f < filters.length; f++) {
            if (filters[f].type.toLowerCase() === d.resources[i].tags[t].ttype.toLowerCase()) {
                if (filters[f].sels.length > 0) {
                    for (var s = 0; s < filters[f].sels.length; s++) {
                        var csel = filters[f].sels[s].toLowerCase();
                        for (y = 0; y < d.resources[i].tags[t].items.length; y++) {
                            if (d.resources[i].tags[t].items[y].toLowerCase() === csel)
                                matches[f] = true;
                        }
                    }
                } else {
                    matches[f] = true;
                }
            }
        }
    }
} else {
    for (var f = 0; f < filters.length; f++)
        matches.push(true);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s