traverse
Deeply enumerate an object and any nested objects
Usage
Recursively visit each property of an object (or each element of an array) and its nested objects or arrays. To traverse non-array iterables (e.g. Map
, Set
) and class instances, see the Traversing other objects section.
Traversal is performed in a depth-first manner. That means the deepest object will be visited before the last property of the root object.
Tip: Check out the Advanced section to see what else is possible.
Types
TraverseVisitor
The TraverseVisitor
type represents the function passed to traverse
as its 2nd argument. If you ever need to declare a visitor separate from a traverse
call, you can do so by declaring a function with this type signature.
TraverseContext
Every visit includes a context object typed with TraverseContext
, which contains the following properties:
key
: The current key being visited.parent
: The parent object of the current value.parents
: An array of objects (from parent to child) that the current value is contained by.path
: An array describing the key path to the current value from the root.skip
: A function used for skipping traversal of an object. If no object is provided, the current value is skipped. See Skipping objects for more details.skipped
: A set of objects that have been skipped.value
: The current value being visited.
TraverseOptions
You may set these options for traverse
using an object as its 3rd argument.
ownKeys
: A function that returns the own enumerable property names of an object.rootNeedsVisit
: A boolean indicating whether the root object should be visited.
See the Options section for more details.
Options
Traversing all properties
By default, non-enumerable properties and symbol properties are skipped. You can pass in a custom ownKeys
implementation to control which object properties are visited.
This example shows how Reflect.ownKeys
can be used to include non-enumerable properties and symbol properties. Note that symbol properties are always traversed last when using Reflect.ownKeys
.
Visiting the root object
By default, your visitor
callback will never receive the object passed into traverse
. To override this behavior, set the rootNeedsVisit
option to true.
When the root object is visited, the key
will be null
.
Advanced
Traversing other objects
If traversing plain objects and arrays isn’t enough, try calling traverse
from within another traverse
callback like follows. This takes advantage of the fact that the root object is always traversed.
If you didn’t set any options, the options
argument can be null:
Skipping objects
Using the TraverseContext::skip
method, you can prevent an object from being traversed. By calling skip()
with no arguments, the current value won’t be traversed.
You can pass any object to skip()
to skip traversal of that object.
Exiting early
If your visitor
callback returns false, traverse
will exit early and also return false. This is useful if you found what you wanted, so you don’t need to traverse the rest of the objects.
Leave callbacks
If your visitor
callback returns a function, it will be called once traverse
has visited every visitable property/element within the current object. This is known as a “leave callback”.
Your leave callback can return false to exit traversal early.