Checking objects inside objects in Javascipt

Hello all, can anyone help me out with figuring out how I can check for objects inside objects and comparing their values?

Here’s my code.

function check(obj1, obj2) {
    props1 = Object.getOwnPropertyNames(obj1);
    props2 = Object.getOwnPropertyNames(obj2);
  
    if (props1.length !== props2.length) {
        return false;
    }
  
    for (let i=0; i < props1.length; i++) {
        let prop = props1[i];

        
        if (typeof obj1[prop] === 'object'){
            check(obj1[prop], obj2[prop]);
        }
        
        if (obj1[prop] !== obj2[prop])
            return false;
        
        }
  
    return true;
  }
  
  
  console.log(check({a: 1, b: {d: 1, e: 2}}, {a: 1, b: {d: 1, e: 2}}));

first you need to declare all variables
you never declare props1 and props2

second

this is not doing anything. You have a function call, but it doesn’t do anything, you don’t do anything with the output value.

fix these, then you can check if your logic works

1 Like

Got it, thanks! Declared the variables, checked for the return type of my ‘check’ function and converted two if’s inside my for loop to if else. Code is working now :smile: