DEV Community

Cover image for Deep Equal
Kenny
Kenny

Posted on

Deep Equal

Hi Everyone, This is week 3 of our bootcamp and we're presented with a few more toy problems! This weeks toy problem to took me a while to figure out, until I ran into the into a rather cheeky way of solving it, but unfortunately the constraint was NOT to use JSON.Stringify. We're going to break down the Deep Equal toy problem.

Lets get started!

const deepEquals = (obj1, obj2) => {
};

This toy problem takes in two objects and its output a boolean that determines if the two objects are deeply equivalent, which means they're both the same or not.

From here within the function we can declare a boolean variable which will be our final returned product:

const deepEquals = (obj1, obj2) => {
   let boolResult = true;
};

Our boolResult will be true for now, until we do a little bit more testing we can set it accordingly.

Next lets get the keys of both obj1 and obj2!
instead of proceeding with a for in loop we can just use Object.keys on them and store them into variables!

 const deepEquals = (obj1, obj2) => {
   let boolResult = true;
   let ob1 = Object.keys(obj1);
   let ob2 = Object.keys(obj2);
};

Now that we have both of the object's keys store in their respective variables we have one more variable to declare!
Next we're going to store a ternary into a variable to that'll test the which object key to iterate through.
The following ternary variable will test the length of object 1's keys if it is greater than or equal to object 2's keys.

 const deepEquals = (obj1, obj2) => {
   let boolResult = true;
   let ob1 = Object.keys(obj1);
   let ob2 = Object.keys(obj2);
   let objKeys = ob1.length >= ob2.length ? ob1 : ob2;

With the the last variable in play we can start the testing process!
We can start iterating through the objKeys that can be either object 1 or 2's keys. By calling forEach on the objKeys we can get the current key of the object.
Afterwards in our forEach we will test the type of both objects with its current key to equal to object. Next to be chained with its NOT recursive call

  objKeys.forEach((currentKey) => {
    if(typeof obj1[currentKey] === 'object' && typeof obj2[currentKey] === 'object'){
      if(!deepEquals(obj1[currentKey], obj2[currentKey])){
        boolResult = false;
      }

If the objects current key did not equal we'll re-initialize our boolResult to false, That will be later to be returned, We're almost done we've got an else statement to set to just test if the objects current key does NOT equal to each other and also re-initialize our boolResult to be false.

lastly we can finally return our boolResult that can be either true or false.
Our final should look something similar to this:

const deepEquals = (obj1, obj2) => {
  let boolResult = true;
    let ob1 = Object.keys(obj1);
    let ob2 = Object.keys(obj2);
    let objKeys = ob1.length >= ob2.length ? ob1 : ob2;
  objKeys.forEach((currentKey) => {
    if(typeof obj1[currentKey] === 'object' && typeof obj2[currentKey] === 'object'){
      if(!deepEquals(obj1[currentKey], obj2[currentKey])){
        boolResult = false;
      }
    } else {
     if(obj1[currentKey] !== obj2[currentKey]){
       boolResult = false;
     } 
    }
  });
  return boolResult;

If there's a much simpler way to solve this toy problem, besides using JSON.Stringify I'd love to learn a different approach on how to tackle varies toy problems!

Thank you for taking the time in having a look at my post! til next time!

Top comments (0)