json - JavaScript: Stringify a shallow copy of a circular referenced object? -
is there way shallow copy of below 1 layer deep? have means fix using different design wondering if else had ran trying convert string before.
var someobjclass = function() { var self = this; this.group = { members: [self] }; }; var p = new someobjclass(); var str = json.stringify(p);
it's little unclear you're asking, if goal stringify circular object, you'll have override tojson
specify how you'd object represented
function someobjclass () { var self = this; this.group = { members: [self] }; } someobjclass.prototype.addmember = function(m) { this.group.members.push(m); }; // when stringifying object, don't include `self` in group.members someobjclass.prototype.tojson = function() { var self = this; return { group: { members: self.group.members.filter(function (x) { return x !== self }) } }; } var = new someobjclass(); var b = new someobjclass(); a.addmember(b); console.log(json.stringify(a))
this best can without seeing more of code. don't know how you're using code, whatever case, not best design class. if share rest of class , code uses it, can more effectively.
Comments
Post a Comment