[removed] Deep check objects have same keys

2019-06-23 19:17发布

问题:

Question is similar to: How can I check that two objects have the same set of property names? but only one difference

I want to check:

var objOne = {"a":"one","b":"two","c":{"f":"three_one"}};
var objTwo = {"a":"four","b":"five","c":{"f":"six_one"}};

have the same set of keys in all level?

For example deepCheckObjKeys(objOne, objTwo) would return true where deepCheckObjKeys(objOne, objThree) return false, if:

var objThree = {"a":"four","b":"five","c":{"g":"six_one"}};

Since objThree.a.c.f is undefined in objThree.

A function like this:

'use strict';

function objectsHaveSameKeys() {
   for (var _len = arguments.length, objects = Array(_len), _key = 0; _key < _len; _key++) {
      objects[_key] = arguments[_key];
   }

   var allKeys = objects.reduce(function (keys, object) {
      return keys.concat(Object.keys(object));
   }, []);
   var union = new Set(allKeys);
   return objects.every(function (object) {
      return union.size === Object.keys(object).length;
   });
}

only checks the first level.

PS: objectsHaveSameKeys() ES6 equivalent:

function objectsHaveSameKeys(...objects):boolean {
   const allKeys = objects.reduce((keys, object) => keys.concat(Object.keys(object)), []);
   const union = new Set(allKeys);
   return objects.every(object => union.size === Object.keys(object).length);
}

回答1:

I'd just do a recursive check if a property's value is an object; see comments:

const deepSameKeys = (o1, o2) => {
  // Get the keys of each object
  const o1keys = Object.keys(o1).sort();
  const o2keys = Object.keys(o2).sort();
  // Make sure they match
  // If you don't want a string check, you could do
  // if (o1keys.length !== o2keys.length || !o1keys.every((key, index) => o2keys[index] === key)) {
  if (o1keys.join() !== o2keys.join()) {
    // This level doesn't have the same keys
    return false;
  }
  // Check any objects
  return o1keys.every(key => {
    const v1 = o1[key];
    const v2 = o2[key];
    if (v1 === null) {
      return v2 === null;
    }
    const t1 = typeof v1;
    const t2 = typeof v2;
    if (t1 !== t2) {
      return false;
    }
    return t1 === "object" ? deepSameKeys(v1, v2) : true;
  });
};
var objOne = {"a":"one","b":"two","c":{"f":"three_one"}};
var objTwo = {"a":"four","b":"five","c":{"f":"six_one"}};
var objThree = {"a":"four","b":"five","c":{"g":"six_one"}};

console.log("objOne, objTwo: " + deepSameKeys(objOne, objTwo));
console.log("objTwo, objThree: " + deepSameKeys(objTwo, objThree));



回答2:

You can create recursive function that will return all keys and check if they are equal with every().

var objOne = {"a":"one","b":"two","c":{"f":"three_one"}};
var objTwo = {"a":"four","b":"five","c":{"f":"six_one"}};

function checkKeys(obj1, obj2) {

  function inner(obj) {
    var result = []

    function rec(obj, c) {
      Object.keys(obj).forEach(function(e) {
        if (typeof obj[e] == 'object') rec(obj[e], c + e)
        result.push(c + e)
      })
    }
    rec(obj, '')
    return result
  }

  var keys1 = inner(obj1), keys2 = inner(obj2)
  return keys1.every(e => keys2.includes(e) && keys1.length == keys2.length)
}

console.log(checkKeys(objOne, objTwo))