A javascript library for working with objects
Traverses a nested object. Circular objects are only traversed once.
Returns: boolean
- True if the callback function returns a truthy value for any path, otherwise false.
Param | Type | Default | Description |
---|---|---|---|
object | object |
The object to traverse. | |
callback | function |
Provides two args, path and value. If true is returned then stop traversing and return true. | |
[isOptimistic] | boolean |
false |
If true then returning true in the callback will prevent going deeper down that branch, but will otherwise continue traversing. |
Example
import { traverse } from 'object-agent';
const thing = {
a: [{
b: 'c'
}, {
b: 'd'
}],
e: 'f
};
traverse(thing, (path, value) => {
console.log(path, value);
});
// => '', { a: [{ b: 'c' }, { b: 'd' }] }
// => 'a', [{ b: 'c' }, { b: 'd' }]
// => 'a.0', { b: 'c' }
// => 'a.0.b', 'c'
// => 'a.1', { b: 'd' }
// => 'a.1.b', 'd'
// => 'e', 'f'