Last active
August 29, 2015 14:01
-
-
Save ChaseWest/dcc66d6cd767a227238a to your computer and use it in GitHub Desktop.
Loop function to determine the loop type by the object passed in. Set - (Array/Object), fn - (function to call on each iteration), order - (if order doesn't matter and set is an array, then use a while loop)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loop(set, fn, order){ | |
var is_array = Object.prototype.toString.call(set) === '[object Array]', | |
i, l; | |
if(is_array){ | |
l = set.length; | |
if (!order) { | |
while(l--){ fn(l); } | |
} | |
else { | |
for(i=0;i<l;i++){ fn(i) } | |
} | |
} else { | |
for(i in set){ | |
if(set.hasOwnProperty(i)){ | |
fn(i); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment