Skip to content

Commit 73d3562

Browse files
#28 parent path key gen. code cleanup in flattenObject()
1 parent b8a29b3 commit 73d3562

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/utils/json.utils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,17 @@ const logger: Logger = new Logger(`json.utils:`, config.logLevel);
1010
* Flattens objects with nested properties for data view display.
1111
* @param obj Object to flatten.
1212
* @param preservePath Optional flag for generating key path.
13-
* @param parentPath Parent key path.
1413
* @returns Flat Object.
1514
*/
16-
export function flattenObject (obj: any, preservePath: boolean = false, parentPath: string = ''): any {
15+
export function flattenObject (obj: any, preservePath: boolean = false): any {
1716
const flatObject: any = {};
1817
Object.keys(obj).forEach((key) => {
19-
if (preservePath) {
20-
if (parentPath.length > 0) {
21-
parentPath = `${parentPath}.${key}`;
22-
} else {
23-
parentPath = key;
24-
}
25-
}
2618
if (typeof obj[key] === 'object' && obj[key] !== null) {
2719
let children: any = {};
28-
Object.assign(children, this.flattenObject(obj[key], preservePath, parentPath));
20+
Object.assign(children, this.flattenObject(obj[key], preservePath));
2921
Object.keys(children).forEach(childKey => {
30-
flatObject[`${parentPath}.${childKey}`] = children[childKey];
22+
const propertyName: string = (preservePath) ? `${key}.${childKey}`: childKey;
23+
flatObject[propertyName] = children[childKey];
3124
});
3225
}
3326
else if (Array.isArray(obj[key])) {

0 commit comments

Comments
 (0)