Skip to content

Instantly share code, notes, and snippets.

@bh-lay
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save bh-lay/350493ff500f668f93db to your computer and use it in GitHub Desktop.

Select an option

Save bh-lay/350493ff500f668f93db to your computer and use it in GitHub Desktop.
Traversal array or object
/**
* 遍历数组
*
*/
function each(arr,fn){
//检测输入的值
if(typeof(arr) == 'object' && typeof(fn) == 'function'){
var Length = arr.length;
if(Length && Length == +Length){
for(var i=0;i<Length;i++){
fn.call(this,i,arr[i]);
}
}else{
for(var i in arr){
if (!arr.hasOwnProperty(i)){
continue;
}
fn.call(this,i,arr[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment