Gathering detailed insights and metrics for object-traversal
Gathering detailed insights and metrics for object-traversal
Gathering detailed insights and metrics for object-traversal
Gathering detailed insights and metrics for object-traversal
jsonpath
Query JavaScript objects with JSONPath expressions. Robust / safe JSONPath engine for Node.js.
recursive-iterator
It iterates through a graph or a tree recursively
object-scan
Traverse object hierarchies using matching and callbacks.
configurable-tree-traversal
Very configurable tree traversal for TypeScript
Flexible and performant utility for traversing javascript objects.
npm install object-traversal
Typescript
Module System
Min. Node Version
TypeScript (83.36%)
JavaScript (16.64%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
24 Stars
256 Commits
2 Forks
1 Watchers
10 Branches
3 Contributors
Updated on Mar 19, 2025
Latest Version
1.0.1
Package Id
object-traversal@1.0.1
Unpacked Size
68.13 kB
Size
13.44 kB
File Count
27
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
npm i object-traversal
npm run benchmark
)traversalOpts
for even more speed, traversal order, maxDepth and more.1import { traverse } from 'object-traversal'; 2 3traverse(object, callback, opts?);
Any instance of javascript object, cyclic or otherwise.
A function that will be called once for each node in the provided root object
, including the root object
itself.
The callback
function has the following signature:
1// Callback function signature 2export type TraversalCallback = (context: TraversalCallbackContext) => any; 3 4// Callback context 5export type TraversalCallbackContext = { 6 parent: ArbitraryObject | null; // parent is null when callback is being called on the root `object` 7 key: string | null; // key is null when callback is being called on the root `object` 8 value: any; 9 meta: { 10 nodePath?: string | null; 11 visitedNodes: WeakSet<ArbitraryObject>; 12 depth: number; 13 }; 14};
An optional configuration object. See below for the available options and their default values.
1export type TraversalOpts = { 2 /** 3 * Default: 'depth-first' 4 */ 5 traversalType?: 'depth-first' | 'breadth-first'; 6 7 /** 8 * Traversal stops when the traversed node count reaches this value. 9 * 10 * Default: Number.Infinity 11 */ 12 maxNodes?: number; 13 14 /** 15 * If set to `true`, prevents infinite loops by not re-visiting repeated nodes. 16 * 17 * Default: true 18 */ 19 cycleHandling?: boolean; 20 21 /** 22 * The maximum depth that must be traversed. 23 * 24 * Root object has depth 0. 25 * 26 * Default: Number.Infinity 27 */ 28 maxDepth?: number; 29 30 /** 31 * If true, traversal will stop as soon as the callback returns a truthy value. 32 * 33 * This is useful for search use cases, where you typically want to skip traversing the remaining nodes once the target is found. 34 * 35 * Default: false 36 */ 37 haltOnTruthy?: boolean; 38 39 /** 40 * The string to be used as separator for the `meta.nodePath` segments. 41 * 42 * Set to null if you wish to turn off `meta.nodePath` to increase traversal speed. 43 * 44 * Default: '.' 45 */ 46 pathSeparator?: string | null; 47};
1exampleObject = { 2 name: 'Hello World!', 3 age: 1, 4 accounts: 2, 5 friends: 3, 6};
1function double({ parent, key, value, meta }) { 2 if (typeof value === 'number') { 3 parent[key] = value * 2; 4 } 5} 6 7traverse(exampleObject, double); 8 9console.log(exampleObject); 10// { 11// name: 'Hello World!', 12// age: 2, 13// accounts: { checking: 4, savings: 6 }, 14// friends: 8 15// }
1network = { 2 name: 'Person1', 3 age: 52, 4 friends: [ 5 { 6 name: 'Person2', 7 age: 25, 8 friends: [], 9 }, 10 { 11 name: 'Person3', 12 age: 42, 13 friends: [ 14 { 15 name: 'Person4', 16 age: 18, 17 friends: [ 18 { 19 name: 'Person5', 20 age: 33, 21 friends: [], 22 }, 23 ], 24 }, 25 ], 26 }, 27 ], 28};
1const numbersOver25 = []; 2 3function collectOver25({ parent, key, value, meta }) { 4 if (key === 'age' && value > 25) { 5 numbersOver25.push(value); 6 } 7} 8 9traverse(network, collectOver25); 10 11console.log(numbersOver25); 12// [ 52, 42, 33 ]
1network = { 2 name: 'Alice Doe', 3 age: 52, 4 friends: [ 5 { 6 name: 'John Doe', 7 age: 25, 8 friends: [], 9 }, 10 { 11 name: 'Bob Doe', 12 age: 42, 13 friends: [ 14 { 15 name: 'John Smith', 16 age: 18, 17 friends: [ 18 { 19 name: 'Charlie Doe', 20 age: 33, 21 friends: [], 22 }, 23 ], 24 }, 25 ], 26 }, 27 ], 28};
1const pathsToPeopleNamedJohn = []; 2 3function callback({ parent, key, value, meta }) { 4 if (value.name && value.name.startsWith('John')) { 5 pathsToPeopleNamedJohn.push(meta.nodePath); 6 } 7} 8 9traverse(network, callback); 10 11console.log(pathsToPeopleNamedJohn); 12// [ 'friends.0', 'friends.1.friends.0' ]
1network = { 2 name: 'Alice Doe', 3 age: 52, 4 friends: [ 5 { 6 name: 'John Doe', 7 age: 25, 8 friends: [], 9 }, 10 { 11 name: 'Bob Doe', 12 age: 42, 13 friends: [ 14 { 15 name: 'John Smith', 16 age: 18, 17 friends: [ 18 { 19 name: 'Charlie Doe', 20 age: 33, 21 friends: [], 22 }, 23 ], 24 }, 25 ], 26 }, 27 ], 28};
1import { getNodeByPath } from 'object-traversal'; 2 3const firstFriend = getNodeByPath(network, 'friends.0'); 4console.log(firstFriend); 5// { name: 'John Doe', age: 25, friends: [] }
1network = { 2 name: 'Person 1', 3 age: 52, 4 friends: [ 5 { 6 name: 'Person 2', 7 age: 42, 8 friends: [ 9 { 10 name: 'Person 4', 11 age: 18, 12 friends: [], 13 }, 14 ], 15 }, 16 { 17 name: 'Person 3', 18 age: 25, 19 friends: [], 20 }, 21 ], 22};
1let names = []; 2 3function getName({ parent, key, value, meta }) { 4 if (value.name) { 5 names.push(value.name); 6 } 7} 8 9traverse(network, getName, { traversalType: 'breadth-first' }); 10 11console.log(names); 12// [ 'Person 1', 'Person 2', 'Person 3', 'Person 4' ]
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/4 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
30 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More