I need to create a function that compares two different models. One model is from the initial state of a form, retrieved from a backend service as a date object. The other model is after conversion in the front end.
function findDateDifferences(obj1, obj2) {
return Object.entries(obj1).reduce((diff, [key, value]) => {
// Check if the property exists in obj2.
if (obj2.hasOwnProperty(key)) {
const val = obj2[key];
// Compare the values of the properties in both objects.
if (val !== value) {
return {
...diff,
[key]: val,
};
}
}
// Return the previous diff object if no differences found.
return diff;
}, {});
}
const x = {
dateOfBirth: "Wed Jan 06 2021 12:00:05 GMT-0700 (Mexican Pacific Standard Time)",
name: "test"
};
const y = {
dateOfBirth: "2021-01-06T12:00:05.357",
name: "test"
};
console.log(findDateDifferences(x, y));
In this scenario, the dates are the same but in different formats. How can I modify the function to recognize them as identical?