We have a javascript object that contains (a) ordinary properties (strings, arrays of other objects, dates), (b) methods, (c) properties that are jQuery objects.
We need to serialize the parent object and all nested objects to a JSON string to send to the server. As part of this process we want to remove properties that the server doesn't need, and any jQuery objects (which won't serialize due to circular references).
We've tried using the "remover" argument of JSON.stringify but it seems to be ignoring nested objects. We have lodash available. What methodology should we be looking at for this?
var Form = function ($elem, config) {
this.$elem = $elem;
this.config = config;
this.init();
};
$.extend(Form.prototype, {
constructor: Form,
config: null,
$elem: null,
categoryid: null,
$sectionModal: null,
sections: [],
$sectionList: null,
addSection: function (data) {
. . .
},
validate: function () {
. . .
},
serialize: function () {
console.log(JSON.stringify(this, function (key, value) {
if (value instanceof jQuery) {
return undefined;
}
return value;
}));
},
init: function () {
this.sections.push(new Section(1, "Section one"));
this.sections.push(new Section(2, "Section two"));
}
});