What is deep-for-each?
The deep-for-each npm package allows you to iterate over all the properties of an object or elements of an array, including nested ones. This is particularly useful for performing operations on deeply nested structures without having to manually traverse each level.
What are deep-for-each's main functionalities?
Iterate over all properties of a nested object
This feature allows you to iterate over all properties of a nested object, including those within nested objects and arrays. The callback function provides the value, key, subject, and path of each property.
const deepForEach = require('deep-for-each');
const obj = {
a: 1,
b: { c: 2, d: { e: 3 } },
f: [4, 5, { g: 6 }]
};
deepForEach(obj, (value, key, subject, path) => {
console.log(`Key: ${key}, Value: ${value}, Path: ${path}`);
});
Modify properties of a nested object
This feature allows you to modify the properties of a nested object. In this example, all numeric values are doubled.
const deepForEach = require('deep-for-each');
const obj = {
a: 1,
b: { c: 2, d: { e: 3 } },
f: [4, 5, { g: 6 }]
};
deepForEach(obj, (value, key, subject) => {
if (typeof value === 'number') {
subject[key] = value * 2;
}
});
console.log(obj);
Other packages similar to deep-for-each
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data structures. It includes a `_.forEach` method that can be used to iterate over properties of an object or elements of an array, but it does not natively support deep iteration. However, you can achieve similar functionality using `_.cloneDeepWith` or custom recursive functions.
deepdash
Deepdash is an extension of Lodash that adds deep iteration capabilities. It provides methods like `deepForEach`, `deepMap`, and `deepFilter` that allow you to work with nested structures more easily. It is a good alternative if you are already using Lodash and need deep iteration functionality.
traverse
The traverse package provides a set of functions for walking and transforming objects. It includes methods like `forEach`, `map`, and `reduce` that work on deeply nested structures. It is a lightweight alternative to deep-for-each with a focus on object traversal and transformation.
deep-for-each
Recursively iterates over arrays and objects. The iteration is made using a deep-first algorithm.
Installation
$ npm install deep-for-each
This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.
Usage
import deepForEach from 'deep-for-each';
deepForEach({
prop1: 'foo',
prop2: ['foo', 'bar'],
prop3: ['foo', 'foo'],
prop4: {
prop5: 'foo',
prop6: 'bar',
},
}, (value, key, subject, path) => {
console.log(`${path}:`, value);
});
Running the example above will print:
prop1: foo
prop2: [ 'foo', 'bar' ]
prop2[0]: foo
prop2[1]: bar
prop3: [ 'foo', 'foo' ]
prop3[0]: foo
prop3[1]: foo
prop4: { prop5: 'foo', prop6: 'bar' }
prop4.prop5: foo
prop4.prop6: bar
Tests
$ npm test
$ npm test -- --watch
License
Released under the MIT License.