Created
June 1, 2024 10:37
-
-
Save Mazday21/cb4ced23837735479e8cc32b09076229 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export default function objectToString(obj, indent = '') { | |
const lines = ['{']; | |
const entries = Object.entries(obj); | |
entries.forEach(([key, value], index) => { | |
if (typeof value === 'object' && value !== null) { | |
const nestedIndent = `${indent} `; | |
const nestedLines = objectToString(value, nestedIndent); | |
lines.push(`${indent} ${key}: ${nestedLines}`); | |
} else { | |
lines.push(`${indent} ${key}: ${value}`); | |
} | |
if (index === entries.length - 1) { | |
lines.push(`${indent}}`); | |
} | |
}); | |
return lines.join('\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment