Skip to content

Instantly share code, notes, and snippets.

@GianfrancoFrau
Created December 27, 2024 19:03
Show Gist options
  • Save GianfrancoFrau/3538bd204190b254ed4454622d94eb4d to your computer and use it in GitHub Desktop.
Save GianfrancoFrau/3538bd204190b254ed4454622d94eb4d to your computer and use it in GitHub Desktop.
const flattenObject = (obj, final, prefix) => {
Object.keys(obj).map((prop) => {
const current = obj[prop];
const p = prefix ? prefix + '.' + prop : prop;
if (typeof current !== "object") {
final[p] = current;
} else if (Array.isArray(current)) {
final[p] = JSON.stringify(current);
} else {
return flattenObject(current, final, p);
}
});
return final;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment