-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a59a2f2
commit fe60528
Showing
2 changed files
with
18 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
/** | ||
* flattenObject | ||
* @description turns a nested object into a one-dimensional object and joins names with dots | ||
* @param obj | ||
* @param prefix | ||
* @returns flattend objects | ||
*/ | ||
export const flattenObject = (obj: any, prefix: string = "") => | ||
// @ts-ignore | ||
Object.keys(obj).reduce((acc, k) => { | ||
const pre = prefix.length ? prefix + '.' : ''; | ||
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k)); | ||
// @ts-ignore | ||
else acc[pre + k] = obj[k]; | ||
if (typeof obj[k] === 'object') { | ||
// purposly mutating acc | ||
Object.assign(acc, flattenObject(obj[k], pre + k)); | ||
} | ||
else { | ||
// @ts-ignore: implicit any | ||
acc[pre + k] = obj[k]; | ||
} | ||
return acc; | ||
}, {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters