Skip to content

Instantly share code, notes, and snippets.

@ChaseWest
Last active August 29, 2015 14:01
Show Gist options
  • Save ChaseWest/dcc66d6cd767a227238a to your computer and use it in GitHub Desktop.
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)
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