replaceCircularRefs is a function that fixes circular objects in-place. A circular object is replaced by "Circular [Object]"
.
const obj = {
a: 1,
}
obj.b = obj // This converts obj into a circular object
JSON.stringify(obj) // This will throw an error because obj is circular
replaceCircularRefs(obj) // This will fix obj in-place
console.log(obj);
// Output:
// {
// a: 1,
// b: "Circular [Object]"
// }
JSON.stringify(obj); // Will not throw error because obj is modified in place
const obj = {
a: 1,
b: {
c: 2
}
}
obj.b.d = obj.b
JSON.stringify(obj) // This will throw an error because obj is circular
replaceCircularRefs(obj)
console.log(obj);
// Output:
// {
// a: 1,
// b: {
// c: 2,
// d: "Circular [Object]"
// }
// }
JSON.stringify(obj); // Will not throw error because obj is modified in place
The task is to implement replaceCircularRefs
function in index.js
as decribed above such that all test cases pass.
replaceCircularRefs
will modify the circular object in-placereplaceCircularRefs
will not modify the object if it is not circular
npm install
npx jest