Using JSON.stringify() on an object that contains cyclical references

I created a Building object that had a 'marker' property (and some other properties). The 'marker' property contained a Google Map Marker, and I'm not sure of it's internals, but when I tried to use JSON.stringify() on it, I got a 'cyclic object value' error. After digging around, I found that you can pass in a second param to stringify(), which is a function that you can use to override the values of properties in the output string. In this case I used it to exclude the marker property from the string.

 

var json = JSON.stringify(buildingObj, function(key,value){
    if(key == "marker"){
        return undefined;
    }
    return value;
});