Skip to content

Latest commit

 

History

History
65 lines (55 loc) · 2.53 KB

traverse.md

File metadata and controls

65 lines (55 loc) · 2.53 KB

Object Agent

A javascript library for working with objects

npm build coverage deps size vulnerabilities license


traverse(object, callback, [isOptimistic]) ⇒ boolean

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'