I was having a problem where JSON.stringify
was not retaining a property I added to an object that was created using $("#myForm").serializeArray();
.
I found this SO thread: JSON.stringify is ignoring object properties
And the solution worked for me -- I created a new object which extended my mySerializedFormArray
object, then I was able to call delete newObj.toJSON
.
If I simply called delete mySerializedFormArray.toJSON;
, the new property I added is still removed during stringification.
Why does it work when I extend the object?
Edit:
var DynExport = $('#frmDynExport').serializeArray();
DynExport.IsFooEnabled = $("#hdnFooFlag").val();
var newObj = $.extend({}, DynExport);
delete newObj.toJSON;
var someOtherVar = JSON.stringify(newObj);
DynExport = JSON.stringify(DynExport);
someOtherVar
will include IsFooEnabled
, DynExport
does not.