JSFiddle - React, Tailwind, and code Playground
by John Wick
JavaScript
var tag = [
{
"id": "7d687db8-00c0-49b9-9d37-9287ba47e89c",
"name": "Bakery"
},
{
"id": "0b683c1d-b3b1-4534-bdff-3fb54e7d1cf3",
"name": "Bar",
"active": true,
"created_at": "2024-07-01T10:45:52.077Z"
}
];
var currentTag = [
{
"id": "7d687db8-00c0-49b9-9d37-9287ba47e89c",
"name": "Bakery"
},
{
"id": "0b683c1d-b3b1-4534-bdff-3fb54e7d1cf3",
"name": "Bar",
"active": true,
"created_at": "2024-07-01T10:45:52.077Z"
}
];
// Function to compare objects
function isEqual(obj1, obj2) {
return Object.keys(obj1).length === Object.keys(obj2).length &&
Object.keys(obj1).every(key => obj1[key] === obj2[key]);
}
// Check if currentTag is equal to any object in tag
var isTagEqual = tag.some(t => isEqual(t, currentTag[0]));
console.log(isTagEqual); // Output: true if they are equal, false otherwise
const diffObjects = (prevObj, newObj) => {
const diff = {};
function compareValues(beforeVal, afterVal) {
if (typeof beforeVal === "object" && !Array.isArray(beforeVal) && beforeVal !== null) {
// Recursively compare if it's an object
let nestedDiff;
if ((beforeVal !== null && beforeVal instanceof Date) ||
(afterVal !== null && afterVal instanceof Date)) {
const beforeDate = moment(beforeVal).format('lll');
const afterDate = moment(afterVal).format('lll');
if (beforeDate !== afterDate) {
return { prev: beforeVal, new: afterVal };
} else {
return;
}
} else {
nestedDiff = diffObjects(beforeVal, afterVal);
}
return Object.keys(nestedDiff).length > 0 ? nestedDiff : null;;
} else if (Array.isArray(beforeVal) && Array.isArray(afterVal)) {
// Compare arrays
if (JSON.stringify(sortMixArry(beforeVal)) !== JSON.stringify(sortMixArry(afterVal))) {
return { prev: beforeVal, new: afterVal };
}
} else {
// Compare primitive values
if ((beforeVal !== null ||...