Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Latest commit

 

History

History
65 lines (55 loc) · 2.47 KB

traverse.md

File metadata and controls

65 lines (55 loc) · 2.47 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.

Returns: Boolean - true if the callback function returns a truthy value for any path; otherwise, false.

Param Type Default Description
object Object
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'