Created
February 21, 2014 06:43
-
-
Save KoryNunn/9129854 to your computer and use it in GitHub Desktop.
Counts the number of unique object instances within an object. It's slow.
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 countObjChildren(thing){ | |
| var visited = []; | |
| function count(thing){ | |
| for(var key in thing){ | |
| var prop = thing[key]; | |
| if(prop != null && typeof thing[key] === 'object'){ | |
| if(visited.indexOf(prop)<0){ | |
| visited.push(prop); | |
| count(prop); | |
| } | |
| } | |
| } | |
| } | |
| count(thing); | |
| return visited.length; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment