Last active
August 29, 2015 14:28
-
-
Save zero-g/10aef7e50a5667ddc312 to your computer and use it in GitHub Desktop.
objectTraverse
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 objectTraverse(obj, fn, isOverWrite, breakKey) { | |
| breakKey = breakKey || 'traverse_break'; | |
| var value, fnResult; | |
| for(var key in obj) { | |
| if(!obj.hasOwnProperty(key)) { | |
| continue; | |
| } | |
| value = obj[key]; | |
| result = fn(key, value); | |
| if(result === breakKey) { | |
| break; | |
| } else { | |
| //复写的key,则不遍历 | |
| if(isOverWrite && result !== undefined && obj[key] !== result) { | |
| obj[key] = result; | |
| } else if(typeof value == 'object') { | |
| obj[key] = arguments.callee.apply(this, [value, fn, isOverWrite, breakKey]); | |
| } | |
| } | |
| } | |
| return obj; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment