walker? idk
This commit is contained in:
		
							parent
							
								
									869b406323
								
							
						
					
					
						commit
						342ca8c5fc
					
				| @ -1,34 +1,39 @@ | |||||||
| 
 | 
 | ||||||
| const set = new Set(); | function Walk(obj) | ||||||
| function Walk(obj, parentObject, propertyName, path=[]) |  | ||||||
| { | { | ||||||
|     if(obj !== null && typeof obj === "object") |     const set = new Set(); | ||||||
|  |     function Step(obj, path="") | ||||||
|     { |     { | ||||||
|         if(set.has(obj)) |         if(obj !== null && typeof obj === "object") | ||||||
|         { |         { | ||||||
|             console.log("ive been to", obj, "quitting"); |             if(set.has(obj)) | ||||||
|             return; |             { | ||||||
|  |                 console.log("ive been to", obj, "quitting"); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 set.add(obj); | ||||||
|  |                 console.log("ive never seen", obj, "here are its fields:"); | ||||||
|  |                 console.log("-----------------"); | ||||||
|  |                 for(let key in obj) | ||||||
|  |                 { | ||||||
|  |                     const val = obj[key]; | ||||||
|  |                     console.log("  >stepping into", path+"."+key); | ||||||
|  |                     Step(val, path+"."+key); | ||||||
|  |                 } | ||||||
|  |                 console.log("-----------------"); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         else |         else | ||||||
|         { |         { | ||||||
|             set.add(obj); |             console.log(obj, "is a leaf"); | ||||||
|             console.log("ive never seen", obj, "here are its fields:"); |  | ||||||
|             console.log("-----------------"); |  | ||||||
|             for(let key in obj) |  | ||||||
|             { |  | ||||||
|                 const val = obj[key]; |  | ||||||
|                 console.log(key, val); |  | ||||||
|                 Walk(val, obj, key); |  | ||||||
|             } |  | ||||||
|             console.log("-----------------"); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     else |     Step(obj); | ||||||
|     { |  | ||||||
|         console.log(obj, "is a leaf") |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| const root = {hey:"1", deep:{}, arr:["a", "b"]}; | 
 | ||||||
|  | const root = {hey:"1", deep:{basement:{closet:{item1:"one", item2:"two"}}}, arr:["a", "b"]}; | ||||||
| root.circ = root; | root.circ = root; | ||||||
| Walk(root); | Walk(root); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user