@rxap/utilities
Version:
A collection of utility functions, types and interfaces.
35 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.flattenObject = flattenObject;
/**
* This function takes an object and flattens it into a single-level object.
* Nested properties are represented with dot notation in the resulting object.
*
* @export
* @function flattenObject
* @param {any} obj - The object to be flattened. This can be of any type, but non-object types will result in an empty object.
* @param {string} [prefix=''] - An optional prefix to prepend to all property keys in the flattened object. Default is an empty string.
*
* @returns {any} - The flattened object. Each key in the returned object represents a path to a property in the original object, with nested properties represented in dot notation. The value of each key is the value of the corresponding property in the original object.
*
* @example
* // returns { 'a.b': 1, 'a.c': 2, d: 3 }
* flattenObject({ a: { b: 1, c: 2 }, d: 3 });
*
* @example
* // returns { 'prefix.a.b': 1, 'prefix.a.c': 2, 'prefix.d': 3 }
* flattenObject({ a: { b: 1, c: 2 }, d: 3 }, 'prefix');
*/
function flattenObject(obj, prefix = '') {
return Object.keys(obj).reduce((acc, k) => {
const pre = prefix.length ? prefix + '.' : '';
if (typeof obj[k] === 'object') {
Object.assign(acc, flattenObject(obj[k], pre + k));
}
else {
acc[pre + k] = obj[k];
}
return acc;
}, {});
}
//# sourceMappingURL=flatten-object.js.map