I have a complex object
{
"original": {
"assetName": "2669937-cherry-blossoms-wallpapers.jpg",
"tags": "",
"id": 5834
},
"uploadState": {
"status": 3
},
"file": {
"isThumbnailable": false,
"name": "2669937-cherry-blossoms-wallpapers.jpg",
"tags": []
},
"customFields": [{
"customFormApplicationId": 2014,
"customFieldId": 1017,
"referenceId": 0,
"referenceType": 0,
"label": "qaa",
"orderId": 0,
"type": 1,
"value": "",
"defaultValue": "",
"properties": "MULTILINE:false|WATERMARK_TEXT:",
"dateCreated": "0001-01-01T00:00:00",
"isRequired": true,
"$$hashKey": "object:22760",
"requiredValueSet": false
}, {
"customFormApplicationId": 2014,
"customFieldId": 1018,
"referenceId": 0,
"referenceType": 0,
"label": "ddd",
"orderId": 1,
"type": 3,
"properties": "MULTILINE:true|WATERMARK_TEXT:|VISIBLE_LINES:5|DISPLAY_TYPE:1|DATE_FORMAT:1|TIME_FORMAT:1",
"dateCreated": "0001-01-01T00:00:00",
"isRequired": true,
"$$hashKey": "object:22761",
"isSet": true,
"value": "",
"requiredValueSet": false
}, {
"customFormApplicationId": 2014,
"customFieldId": 2017,
"referenceId": 0,
"referenceType": 0,
"label": "drop",
"orderId": 2,
"type": 2,
"value": "",
"defaultValue": "",
"properties": "MULTILINE:true|WATERMARK_TEXT:|VISIBLE_LINES:5|ITEMS:v1,v2,v3|DISPLAY_TYPE:1",
"dateCreated": "0001-01-01T00:00:00",
"isRequired": false,
"$$hashKey": "object:22762"
}],
"$$hashKey": "object:16951"
}
with dynamic structures.
I have to deep clone the object and I am using this method
var clone = $.parseJSON(JSON.stringify(original));
This is the only thing that actually worked so no other method can be used. The problem is that Date objects are converted to string So instead of
"dateCreated": Mon Jan 21 2019 13:45:06 GMT-0500 (Eastern Standard Time)
__proto__: Object,
I have "dateCreated":"2019-01-21T18:45:06.696Z"
To convert that back to Date I use
clone.dateCreated = new Date(original.dateCreated )
THE PROBLEM is that my object is very complex and has dynamic properties so I don't know the structure of the object.
What I need is to write function that will run on original object and check each property and if that property is Date type then go to clone to the same property and convert that string to Date
How the function should look like? Two key issues:
1 - run over all properties of the original object and check type 2 - find the same property in clone object
given they have the same structure
I am using ES5 and no lodash or underscore library