JSFiddle - React, Tailwind, and code Playground

by Salmin Skenderovic

JavaScript

const prev = {
  "enabled": true,
  "apiVersion": "1.0.0",
  "mode": 2,
  "features": {
    "upload": {
      "detection": true,
      "prevention": false
    },
    "tab": {
      "detection": false,
      "prevention": false
    }
  }
}


const next = {
  "enabled": true,
  "apiVersion": "1.0.0",
  "mode": 2,
  "features": {
    "upload": {
      "detection": true,
      "prevention": false
    },
    "tab": {
      "detection": false,
      "prevention": false
    }
  }
}


const empty = {}



function findChangedProperties(current, updated) {
  const changedProperties = {};

  for (const key in current) {
  if (updated[key] === undefined) continue;
  
    if (typeof current[key] === 'object' && typeof updated[key] === 'object') {
      const nestedChanges = findChangedProperties(current[key], updated[key]);
      if (Object.keys(nestedChanges).length > 0) {
        changedProperties[key] = nestedChanges;
      }
    } else if (current[key] !== updated[key]) {
      changedProperties[key] = updated[key];
    }
  }

  return changedProperties;
}


console.log(
findChangedProperties(prev, next),
findChangedProperties(prev, empty),
)