Open this example in the interactive library
Source code
const flatten = (obj, prefix = "") =>
Object.entries(obj).flatMap(([key, value]) => {
const path = prefix ? prefix + "." + key : key;
return value && typeof value === "object" && !Array.isArray(value)
? flatten(value, path)
: [[path, value]];
});
console.log(
Object.fromEntries(
flatten({ print: { paper: "ivory", run: 250 }, ready: true }),
),
);
Sample output
{ 'print.paper': 'ivory', 'print.run': 250, ready: true }