diginext-utils
Version:
README.md
32 lines • 992 B
JavaScript
/**
* Recursively iterates through all keys and values of an object and its nested objects.
* Calls the callback function for each key-value pair.
*
* @template T - The type of the object
* @param obj - The object to iterate through
* @param callback - Function to call for each key-value pair
* @returns The original object (potentially modified by callback)
*
* @example
* ```ts
* const obj = { a: 1, b: { c: 2, d: 3 } };
* iterate(obj, (obj, key, value) => {
* console.log(key, value);
* });
* // Logs: 'a' 1, 'b' { c: 2, d: 3 }, 'c' 2, 'd' 3
* ```
*/
export function iterate(obj, callback) {
if (typeof obj !== "object" || obj === null) {
return obj;
}
Object.keys(obj).forEach((key) => {
const value = obj[key];
callback(obj, key, value);
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
iterate(value, callback);
}
});
return obj;
}
//# sourceMappingURL=iterate.js.map