Deleting object properties if they are not found in another object. (javascript)

Let’s take these two objects as an exemple:

let objA = {     key1 : "whatever",     key2 : "whatever",     key3 : "whatever",     key4 : "whatever",     key5 : {         subKey1: "whatever",         subKey2: "whatever"     } } let objB = {     key1 : "whatever",     key2 : "whatever",     key3 : "whatever",     key5 : {         subKey1: "whatever"     } } 

I want to delete all the properties of objA if they are not present in objB. The values of the properties do not matter, only the keys themselves. The challenge, of course, is for it to work with any object, no matter how deeply nested they can be.

I came up with this solution based on this answer to a similar question.

let objA = {     key1 : "whatever",     key2 : "whatever",     key3 : "whatever",     key4 : "whatever",     key5 : {         subKey1: "whatever",         subKey2: "whatever"     } } let objB = {     key1 : "whatever",     key2 : "whatever",     key3 : "whatever",     key5 : {         subKey1: "whatever"     } }  const deepSameKeys = (o1, o2) => {     const o1keys = Object.keys(o1).sort();      return o1keys.every(key => {         const v1 = o1[key];         const v2 = o2[key];          let goDeeper = true;         if (o2[key] == undefined) {             console.log('@found a key to delete: ', key);             delete o1[key];             goDeeper = false;         }         const t1 = typeof v1;         return t1 === "object" && goDeeper ? deepSameKeys(v1, v2) : true;     }); };  let result = deepSameKeys(objA, objB) console.log(result) console.log(objA) // no more key4 nor subKey2

So far so good, but I wonder if someone can come up with something better or a situation where my solution would fail.