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.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
context: _.TraverseContext<string | number | symbol>
context) => {
const
constdepth:number
depth=
context: _.TraverseContext<string | number | symbol>
context.
TraverseContext<string | number | symbol>.parents: readonly object[]
The stack of parent objects. The last object is the current
parent.
⚠️ This array must not be mutated directly or referenced outside
the visitor callback. If that's necessary, you'll want to clone
it first.
parents.
ReadonlyArray<object>.length: number
Gets the length of the array. This is a number one higher than the highest element defined in an array.
length
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Returns a String value that is made from count copies appended together. If count is 0,
the empty string is returned.
@param ― count number of copies to append
repeat(
constdepth:number
depth*2),
key: string | number | symbol
key, '=>',
value: unknown
value)
})
// Logs the following:
// a => { b: 2 }
// b => 2
// c => [1, 2]
// 0 => 1
// 1 => 2
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.
context: TraverseContext<string | number | symbol>
context) => {
// ...
}
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.
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.
import*as
import _
_from'radashi'
const
constsymbol:typeof symbol
symbol=
var Symbol:SymbolConstructor
(description?:string|number) => symbol
Returns a new unique Symbol value.
@param ― description Description of the new Symbol object.
Symbol('b')
const
constroot: {
[symbol]:number;
}
root= { [
constsymbol:typeof symbol
symbol]: 1 }
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Adds a property to an object, or modifies attributes of an existing property.
@param ― o Object on which to add or modify the property. This can be a native JavaScript object (that is, a user-defined object or a built in object) or a DOM object.
@param ― p The property name.
@param ― attributes Descriptor for the property. It can be for a data property or an accessor property.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Returns the string and symbol keys of the own properties of an object. The own properties of an object
are those that are defined directly on that object, and are not inherited from the object's prototype.
@param ― target Object that contains the own properties.
ownKeys },
)
// Logs the following:
// a => 2
// Symbol(b) => 1
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.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
TraverseOptions<Key = string | number | symbol>.rootNeedsVisit?: boolean |null
When true, the visitor callback will be invoked for the root object.
@default ― false
rootNeedsVisit: true },
)
// Logs the following:
// null => { a: 1 }
// a => 1
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.
import*as
import _
_from'radashi'
// Note how we're using a named visitor function so it can reference itself.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
function (localfunction) visitor(value:unknown, key:string|number|symbol, parent:object, context:_.TraverseContext<string|number|symbol>, options:_.TraverseOptions<string|number|symbol> & {
...;
}):boolean|undefined
visitor,
options: _.TraverseOptions<string | number | symbol>& {
rootNeedsVisit?:null;
}
options,
context: _.TraverseContext<string | number | symbol>
context)
}
// TODO: Handle other values as needed.
})
If you didn’t set any options, the options argument can be null:
return _.traverse(root, visitor, null, context)
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.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
context: _.TraverseContext<string | number | symbol>
context) => {
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
context: _.TraverseContext<string | number | symbol>
context) => {
var console:Console
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
Performs the specified action for each element in an array.
@param ― callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
forEach(
nestedValue: unknown
nestedValue=> {
if (
import _
_.
functionisObject(value:unknown):valueisobject
export isObject
Returns true if value is a plain object, a class instance
(excluding built-in classes like Date/RegExp), or an
Object.create(null) result. Objects from other realms are
also supported.
context: _.TraverseContext<string | number | symbol>
context.
TraverseContext<string | number | symbol>.skip: (obj?:object) =>void
When the current value is a traversable object/iterable, this
function can be used to skip traversing it altogether.
If the obj argument is provided, it marks the given object as
skipped (instead of the current value), preventing it from being
traversed.
skip(
nestedValue: object
nestedValue)
}
})
})
// Logs the following:
// a => { b: { c: 1 } }
// b => { c: 1 }
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.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
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.
Recursively visit each property of an object (or each element of an
array) and its nested objects or arrays. By default, the only
nested objects to be traversed are plain objects and arrays.
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).
The console module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
A global console instance configured to write to process.stdout and
process.stderr. The global console can be used without importing the node:console module.
Warning: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the note on process I/O for
more information.
Example using the global console:
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(newError('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
constname='Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
Example using the Console class:
constout=getStreamSomehow();
consterr=getStreamSomehow();
constmyConsole=new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(newError('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
Prints to stdout with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to printf(3)
(the arguments are all passed to util.format()).