3

I have a function which I call to cut of anything beyond a point, but is there any other way to find a reference to itself and remove that reference somehow smartly?

Here is what I am using now:

//Changing the max level will speed things up, but it will mean that some things might not be persisted
function cleanse(Obj, level) {
    var r, i, prims = ["string", "number", "boolean"], maxLevel = 8;
    level = level || 0;
    if (prims.indexOf(typeof Obj) !== -1) {
        r = Obj;
    } else if (Obj instanceof Function) {
        console.log("Please dont reference objects");
        return undefined;
    } else {
        if (Obj instanceof Array) {
            r = [];
        } else {
            r = {};
        }
        for (i in Obj) {
            if (Obj.hasOwnProperty(i)) {
                if (level <= maxLevel) {
                    r[i] = BackStack.cleanse(Obj[i], level + 1);
                }
            }
        }
    }
    return r;
};
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • Underscore.js offers equality testing between objects. http://lodash.com/docs#isEqual – zz3599 Mar 27 '13 at 04:54
  • Im not using underscore and I can't really add to many libraries to the app. – FabianCook Mar 27 '13 at 05:13
  • Perhaps this - http://stackoverflow.com/a/1144249/638127? – zz3599 Mar 27 '13 at 05:14
  • @zz3599 this is one of the rare instances when you actually want to use `objectX === objectX` to check object identity, and don't want something like lodash to detect object equality. – ZachB Jun 01 '16 at 21:29
  • Possible duplicate of [JSON.stringify, avoid TypeError: Converting circular structure to JSON](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json) – FabianCook Jul 11 '17 at 11:00

0 Answers0