-
-
Save bh-lay/350493ff500f668f93db to your computer and use it in GitHub Desktop.
Traversal array or object
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 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