Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

traverse

Package Overview
Dependencies
Maintainers
0
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

traverse

Traverse and transform objects by visiting every node on a recursive walk.

  • 0.2.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.2M
decreased by-11.78%
Maintainers
0
Weekly downloads
 
Created

What is traverse?

The traverse npm package is a comprehensive tool for traversing and transforming JavaScript objects. It allows you to easily navigate through objects, arrays, and other nested structures, and apply functions to their elements. It's particularly useful for manipulating complex data structures, applying transformations, and extracting information in a flexible manner.

What are traverse's main functionalities?

Traversing and modifying objects

This feature allows you to traverse through an object and modify elements. In the code sample, the value 2 in the array under key 'a' is updated to 222.

var traverse = require('traverse');
var obj = { a: [1, 2, 3], b: 4 };
traverse(obj).forEach(function (x) {
    if (x === 2) this.update(222);
});

Cloning and transforming objects

This feature enables deep cloning of objects with the option to apply transformations during the cloning process. In the example, all numbers in the object are multiplied by 100.

var traverse = require('traverse');
var obj = { a: 1, b: [2, 3] };
var clone = traverse(obj).map(function (node) {
    if (typeof node === 'number') this.update(node * 100);
});

Extracting specific elements

This feature is useful for extracting leaf nodes from a complex object structure. The code sample demonstrates how to collect all leaf nodes (elements without child nodes) into an array.

var traverse = require('traverse');
var obj = { a: 1, b: { c: 2, d: [3, 4] } };
var leaves = traverse(obj).reduce(function (acc, x) {
    if (this.isLeaf) acc.push(x);
    return acc;
}, []);

Other packages similar to traverse

FAQs

Package last updated on 19 Feb 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc