Home > Net >  Need to get the updated key, value pairs alone from a mixed object
Need to get the updated key, value pairs alone from a mixed object

Time:08-01

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

CodePudding user response:

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()

CodePudding user response:

Your task can be divided into three parts:

  • get all keys in object
  • write function that can access to properies by dot notation
  • compare values in original and updated objects

It is possible to get all keys in object:

const keyify = (obj, prefix = '') => 
  Object.keys(obj).reduce((res, el) => {
    if( Array.isArray(obj[el]) ) {
      return res;
    } else if( typeof obj[el] === 'object' && obj[el] !== null ) {
      return [...res, ...keyify(obj[el], prefix   el   '.')];
    }
    return [...res, prefix   el];
  }, []);

Write function that can access to properies by dot notation

It is possible to get objects with dot notation like this:

function getDescendantProp(obj, desc) {
    var arr = desc.split(".");
    while(arr.length && (obj = obj[arr.shift()]));
    return obj;
}

And then comparing objects becomes obvious:

const allKeys = keyify(originalObject)
const updatedProperties = []
allKeys.forEach(key => {
    let origValue = getDescendantProp(originalObject, key)
    let updatedValue = getDescendantProp(updatedObject, key)
    if (origValue !== updatedValue)
        updatedProperties.push({key, updatedValue})

})      

The complete example would look like this:

const keyify = (obj, prefix = '') => 
  Object.keys(obj).reduce((res, el) => {
    if( Array.isArray(obj[el]) ) {
      return res;
    } else if( typeof obj[el] === 'object' && obj[el] !== null ) {
      return [...res, ...keyify(obj[el], prefix   el   '.')];
    }
    return [...res, prefix   el];
  }, []);

const getDescendantProp = (obj, desc) => {
    var arr = desc.split(".");
    while(arr.length && (obj = obj[arr.shift()]));
    return obj;
}

 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: '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',
      },
    },
  }

const allKeys = keyify(originalObject)
const updatedProperties = []

allKeys.forEach(key => {
    let origValue = getDescendantProp(originalObject, key)
    let updatedValue = getDescendantProp(updatedObject, key)
    if (origValue !== updatedValue)
        updatedProperties.push({key, updatedValue})

})

console.log(updatedProperties)

  • Related