obsidian-book-tracker/src/utils/flatten.ts

22 lines
443 B
TypeScript

export function flatten(
obj: Record<string, any>,
prefix = ""
): Record<string, any> {
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
const newKey = prefix ? `${prefix}.${key}` : key;
if (
typeof value === "object" &&
value !== null &&
!Array.isArray(value)
) {
Object.assign(acc, flatten(value, newKey));
} else {
acc[newKey] = value;
}
return acc;
}, {} as Record<string, any>);
}