const compare = (obj1, obj2) => {
let isModified = false;
let keyToRemove = ['id', 'updated_at', 'size', 'position'];
let _oldObj = Object.keys(obj1);
let _updatedObj = Object.keys(obj2)
const oldObj = _oldObj.filter(item => keyToRemove.indexOf(item) < 0);
const updatedObj = _updatedObj.filter(item => keyToRemove.indexOf(item) < 0);
if (oldObj.length !== updatedObj.length) {
isModified = true;
} else {
oldObj.forEach(key => {
const oldObjValue = obj1[key],
newObjValue = obj2[key];
if (Array.isArray(oldObjValue) === true) {
if (Array.isArray(newObjValue) === false) {
console.log(oldObjValue + "is Array and" + newObjValue + "is not Array");
isModified = true;
} else {
const allSubObjs = [].concat(
Object.keys(oldObjValue).map(subKey => oldObjValue[subKey]),
Object.keys(newObjValue).map(subKey => newObjValue[subKey])
);
allSubObjs.splice(1).forEach(subObj => {
const recursiveCheck = compare(allSubObjs[0], subObj);
if (recursiveCheck === false) {
console.log("value changed for " + key + " old value " + oldObjValue + " new value " + newObjValue);
isModified = true;
}
});
}
} else if (typeof oldObjValue !== typeof newObjValue) {
console.log("value changed for " + key + " old value " + oldObjValue + " new value " + newObjValue);
isModified = true;
}
});
}
return isModified;
};
const check = (a, b) => {
console.log("==========================");
console.log(`Rug objects ${a} and ${b} are modified? ${compare(a, b)}`);
}
var _existingObj={
"status": "available",
"description": "sdfds s sfs",
"productType": "Tapestry",
"otherTags": [
],
"pattern": "Floral",
"shapeCategoryTags": [
"Square"
],
"sizeCategoryTags": [
"5x8"
],
"_id": "5caf9758cba2cd5606c1bc8b",
"tags": [
{
"name": "ABC",
"_id": "59c57e33b9073f00048e8e8b"
},
{
"name": "XYZ",
"_id": "59c57e44b9073f00048e8e8e"
},
{
"name": "PQR",
"_id": "59c57e3fb9073f00048e8e8d"
}
],
"price": 150000,
"created_at": "2019-04-11T19:36:56.673Z",
"updated_at": "2019-04-12T17:33:50.288Z",
"ID": 1005,
"__v": 0,
"images": [
],
"id": "5caf9758cba2cd5606c1bc8b"
};
var _updatedObj={
"_id": "5caf9758cba2cd5606c1bc8b",
"status": "available",
"description": "sdfds s sfs",
"productType": "Tapestry",
"otherTags": [
],
"pattern": "Floral",
"shapeCategoryTags": [
"Square"
],
"sizeCategoryTags": [
"5x8"
],
"palette": "",
"region": "East Turkestan",
"primaryColor": "",
"styleTags": [
"traditional"
],
"colourTags": [
{
"name": "ABC",
"_id": "59c57e33b9073f00048e8e8b"
},
{
"name": "XYZ",
"_id": "59c57e44b9073f00048e8e8e"
},
{
"name": "QWE",
"_id": "59c57e3fb9073f00048e8e8d"
}
],
"price": 150000,
"created_at": "2019-04-11T19:36:56.673Z",
"updated_at": "2019-04-12T17:49:59.568Z",
"ID": 1005,
"__v": 0,
"imageList": [
]
};
var _cloneOf_existingObj={
"status": "available",
"description": "sdfds s sfs",
"productType": "Tapestry",
"otherTags": [
],
"pattern": "Floral",
"shapeCategoryTags": [
"Square"
],
"sizeCategoryTags": [
"5x8"
],
"_id": "5caf9758cba2cd5606c1bc8b",
"tags": [
{
"name": "ABC",
"_id": "59c57e33b9073f00048e8e8b"
},
{
"name": "XYZ",
"_id": "59c57e44b9073f00048e8e8e"
},
{
"name": "PQR",
"_id": "59c57e3fb9073f00048e8e8d"
}
],
"price": 150000,
"created_at": "2019-04-11T19:36:56.673Z",
"updated_at": "2019-04-12T17:33:50.288Z",
"ID": 1005,
"__v": 0,
"images": [
],
"id": "5caf9758cba2cd5606c1bc8b"
};
check(_existingObj, _updatedObj);
check(_existingObj,_cloneOf_existingObj);