traverse
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The traverse
npm package allows for the traversal and transformation of JavaScript objects. It conducts a recursive walk, visiting each element or node within the object, and provides a variety of methods for interaction and manipulation with these nodes. This can be useful for a wide range of operations, such as transforming values, collecting leaf nodes, removing circular references, and much more.
You can use traverse
in your JavaScript projects by installing it via npm using the command npm install traverse
. Here's an example of how to use traverse to transform negative numbers in an array into their equivalent positive numbers:
var traverse = require('traverse');
var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
traverse(obj).forEach(function (x) {
if (x < 0) this.update(x + 128);
});
console.dir(obj);
In the above example, traverse(obj).forEach(function (x) {...})
is looping through each element ('x') in the object, checking if it's less than zero (a negative number), and if so, updating that element in-place to transform it from a negative to a positive number by adding 128 to it.
The traverse
documentation can be found directly in the README file on its GitHub repository, which is located at https://github.com/ljharb/traverse. The documentation includes a list of all the available methods, their descriptions, usage examples, and details about their method-specific context values.