Passing an object with circular references from server to client-side Javascript while retaining circularity

后端 未结 2 496
栀梦
栀梦 2020-12-05 21:47

I\'m trying to pass an object with circular references from node.js server to client-side javascript.

Server (node.js):

var object = { circular: obje         


        
2条回答
  •  甜味超标
    2020-12-05 21:51

    Douglas Crockford has a solution for this that I have successfully used to solve this problem before: Cycle.js

    instead of just using stringify and parse you would first call decycle and restore with retrocycle

    var jsonString = JSON.stringify(JSON.decycle(parent));
    var restoredObject = JSON.retrocycle(JSON.parse(jsonString));
    

    JSFiddle

提交回复
热议问题