0

I have 2 objects, one original and one updated. The object has many nested objects. i need to get the updated key-value pairs alone

originalObject = {
  alphaKey: "alphaValue",
  betaKey: {
    betaAlphaKey: "betaAlphaValue",
    betaBetaKey: 123456,
  },
  gammaKey: "gammaValue",
  deltaKey: {
    deltaAlphaKey: "deltaAlphaValue",
    deltaBetaKey: {
      deltaBetaAlphaKey: "deltaBetaAlphaValue",
      deltaBetaBetaKey: "deltaBetaBetaValue",
    },
    deltaGammaKey: "deltaGammaValue",
  },
  epsilonKey: "epsilonValue",
  zetaKey: {
    zetaAlphaKey: "zetaAlphaValue",
    zetaBetaKey: 111222,
    zetaGammaKey: {
      zetaGammaAlphaKey: "zetaGammaAlphaValue",
      zetaGammaBetakey: "zetaGammaBetaValue",
      zetaGammaGammaKey: 222333444,
    },
  },
};

and

updatedObject = {
  alphaKey: "alphaValue",
  betaKey: {
    betaAlphaKey: "betaAlphaValue",
    betaBetaKey: 000123456,
  },
  gammaKey: "gammaValue",
  deltaKey: {
    deltaAlphaKey: "updatedDeltaAlphaValue",
    deltaBetaKey: {
      deltaBetaAlphaKey: "deltaBetaAlphaValue",
      deltaBetaBetaKey: "updatedDeltaBetaBetaValue",
    },
    deltaGammaKey: "deltaGammaValue",
  },
  epsilonKey: "epsilonValue",
  zetaKey: {
    zetaAlphaKey: "updatedZetaAlphaValue",
    zetaBetaKey: 000111222,
    zetaGammaKey: {
      zetaGammaAlphaKey: "zetaGammaAlphaValue",
      zetaGammaBetakey: "updatedZetaGammaBetaValue",
      zetaGammaGammaKey: 011222333444,
    },
  },
};

i want to compare these 2 and get output the upadted fields alone

output = {
    betaBetaKey: 000123456,
    deltaAlphaKey: "updatedDeltaAlphaValue",
    deltaBetaBetaKey: "updatedDeltaBetaBetaValue",
    zetaAlphaKey: "updatedZetaAlphaValue",
    zetaBetaKey: 000111222,
    zetaGammaBetakey: "updatedZetaGammaBetaValue",
    zetaGammaGammaKey: 011222333444
}

so, can you please help me in this

The code is expected to retrieve the nested objects, compare them with original fields, and save the updated fields in output Thanks

Sai
  • 3
  • 2

1 Answers1

0

const originalObject = {
  alphaKey: "alphaValue",
  betaKey: {
    betaAlphaKey: "betaAlphaValue",
    betaBetaKey: 123456,
  },
  gammaKey: "gammaValue",
  deltaKey: {
    deltaAlphaKey: "deltaAlphaValue",
    deltaBetaKey: {
      deltaBetaAlphaKey: "deltaBetaAlphaValue",
      deltaBetaBetaKey: "deltaBetaBetaValue",
    },
    deltaGammaKey: "deltaGammaValue",
  },
  epsilonKey: "epsilonValue",
  zetaKey: {
    zetaAlphaKey: "zetaAlphaValue",
    zetaBetaKey: 111222,
    zetaGammaKey: {
      zetaGammaAlphaKey: "zetaGammaAlphaValue",
      zetaGammaBetakey: "zetaGammaBetaValue",
      zetaGammaGammaKey: 222333444,
    },
  },
};

const updatedObject = {
  alphaKey: "alphaValue",
  betaKey: {
    betaAlphaKey: "betaAlphaValue",
    betaBetaKey: 1337,
  },
  gammaKey: "gammaValue",
  deltaKey: {
    deltaAlphaKey: "updatedDeltaAlphaValue",
    deltaBetaKey: {
      deltaBetaAlphaKey: "deltaBetaAlphaValue",
      deltaBetaBetaKey: "updatedDeltaBetaBetaValue",
    },
    deltaGammaKey: "deltaGammaValue",
  },
  epsilonKey: "epsilonValue",
  zetaKey: {
    zetaAlphaKey: "updatedZetaAlphaValue",
    zetaBetaKey: 777,
    zetaGammaKey: {
      zetaGammaAlphaKey: "zetaGammaAlphaValue",
      zetaGammaBetakey: "updatedZetaGammaBetaValue",
      zetaGammaGammaKey: 111,
    },
  },
};

const getOneObject = async (obj) => {
    const result = {}
     const getKeysOfObject = async (o, key) => {
        if(typeof o === "object"){
            const keys = Object.keys(o)
            for (let i = 0; i < keys.length; i++) {
                await getKeysOfObject(o[keys[i]], keys[i])
            }
        }else{
            result[key] = o
        }
    }
    await getKeysOfObject(obj)
    return result
}

const getDiffs = async () => {
    const obj1 = await getOneObject(originalObject)
    const obj2 = await getOneObject(updatedObject)
    const keys = Object.keys(obj1)
    const output = {}
    for (let i = 0; i < keys.length; i++) {
        if(obj1[keys[i]] !== obj2[keys[i]]) output[keys[i]] = obj2[keys[i]]
    }
    console.log("Output: ", output)
    return output
}

getDiffs()
Nikita Aplin
  • 367
  • 2
  • 10