function prettyPrint(obj) {
const format = (obj, depth = 0, seen = new WeakSet()) => {
if (obj === null) return 'null';
if (typeof obj !== 'object') return String(obj);
if (seen.has(obj)) return '[Circular]';
seen.add(obj);
const indent = ' '.repeat(depth);
const entries = Array.isArray(obj)
? obj.map((v, i) => `[${i}] ${format(v, depth + 1, seen)}`)
: Object.entries(obj).map(([k, v]) => `${k}: ${format(v, depth + 1, seen)}`);
return `${Array.isArray(obj) ? `Array(${obj.length})` : 'Object'} {\n` +
entries.map(e => `${indent} ${e}`).join('\n') + `\n${indent}}`;
};
console.log(format(obj));
}
// 테스트 코드
const a = { name: "Alice", age: 25, nested: { hobby: "Reading" }, items: [1, 2, 3] };
const b = { name: "Alice", age: 25, nested: { hobby: "Reading" }, items: [1, 2, 3] };
b.ref = b; // 순환 참조 생성
prettyPrint(a);
prettyPrint(b);