This function will work for array objects if you face error:
Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'Object' | property 'fields' -> error:
function copyWithCircularRefs(input) {
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
// Instead of returning undefined, return a custom placeholder
return '[Circular]';
}
seen.add(value);
}
return value;
};
};
// Use the custom replacer function with JSON.stringify
const stringifiedData = JSON.stringify(input, getCircularReplacer(), 2);
// Copy the stringified data to the clipboard
copy(stringifiedData);
// Log a message to the console to confirm the action
console.log('Object copied to clipboard');
}
// Usage:
// Replace 'myComplexObject' with your actual complex object
copyWithCircularRefs(myComplexObject);
This function will now handle circular references by replacing them with the string '[Circular]'. When you paste the copied data, you'll see this placeholder wherever a circular reference was detected, which allows you to understand the structure without losing the reference information.