JSFiddle - React, Tailwind, and code Playground
JavaScript
$(function(){
var child = {};
var parent = {child : child};
child.parent = parent;
console.log(parent);
try{
var string = JSON.stringify(parent);
console.log(string);
console.log(JSON.parse(string));
}
catch(e){
console.log(e);
}
var string = JSON.stringify(JSON.decycle(parent));
console.log(string);
console.log(JSON.retrocycle(JSON.parse(string)));
});
// cycle.js
// 2011-02-23
/*jslint evil: true, regexp: false */
/*members $ref, apply, call, decycle, hasOwnProperty, length, prototype, push,
retrocycle, stringify, test, toString
*/
if (typeof JSON.decycle !== 'function') {
JSON.decycle = function decycle(object) {
"use strict";
// Make a deep copy of an object or array, assuring that there is at most
// one instance of each object or array in the resulting structure. The
// duplicate references (which might be forming cycles) are replaced with
// an object of the form
// {$ref: PATH}
// where the PATH is a JSONPath string that locates the first occurance.
// So,
// var a = [];
// a[0] = a;
// return JSON.stringify(JSON.decycle(a));
// produces the string '[{"$ref":"$"}]'.
// JSONPath is used to locate the unique object. $ indicates the top level of
// the object or array. [NUMBER] or [STRING] indicates a child member or
// property.
var objects = [], // Keep a reference to each unique object or array
paths = []; // Keep the path to each unique object or array
return (function derez(value, path) {
// The derez recurses through the object, producing the deep copy.
var i, // The loop counter
name, // Property name
nu; // The new object or array
switch (typeof value) {
case 'object':
// typeof null === 'object', so get out if this value is not really an object.
if (!value) {
...