Flatten Nested Object
Flattens a nested object into a single-level object with dot notation for keys.
- object
- flatten
Install
npx shadcn@latest add https://quicksnip.dev/r/flatten-nested-object.jsonThis snippet is not verified yet. Review it before use, or help verify it.
Code
function flattenObject(obj, prefix = '') {
return Object.keys(obj).reduce((acc, key) => {
const fullPath = prefix ? `${prefix}.${key}` : key;
if (typeof obj[key] === 'object' && obj[key] !== null) {
Object.assign(acc, flattenObject(obj[key], fullPath));
} else {
acc[fullPath] = obj[key];
}
return acc;
}, {});
}Usage
const nestedObj = { a: { b: { c: 1 }, d: 2 }, e: 3 };
flattenObject(nestedObj); // Returns: { 'a.b.c': 1, 'a.d': 2, e: 3 }