JavaScript:深层检查对象具有相同的密钥

时间:2017-01-23 08:51:36

标签: javascript key javascript-objects

问题类似于:How can I check that two objects have the same set of property names?但只有一个差异

我想查一下:

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

在所有级别都有相同的

例如deepCheckObjKeys(objOne, objTwo)会返回true deepCheckObjKeys(objOne, objThree)返回false,如果:{/ p>

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

由于 objThree 中的objThree.a.c.fundefined

这样的功能:

'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;
   });
}

只检查第一级。

PS:objectsHaveSameKeys()等效于ES6:

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);
}

3 个答案:

答案 0 :(得分:6)

我只是做一个递归检查,如果一个属性的值是一个对象;见评论:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以创建递归函数,该函数将返回所有键并检查它们是否与every()相等。

&#13;
&#13;
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))
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我猜您正在寻找 [此处] 提供的函数的深度检查版本 :)(How can I check that two objects have the same set of property names?).

以下是我的尝试。请注意:

  • 解决方案不检查 null 并且不是防弹
  • 我还没有对其进行性能测试。也许 OP 或其他任何人都可以这样做并为社区分享。
  • 我不是 JS 专家:)
function objectsHaveSameKeys(...objects) {
  const allKeys = objects.reduce((keys, object) => keys.concat(Object.keys(object)), [])
  const union = new Set(allKeys)
  if (union.size === 0) return true
  if (!objects.every((object) => union.size === Object.keys(object).length)) return false

  for (let key of union.keys()) {
    let res = objects.map((o) => (typeof o[key] === 'object' ? o[key] : {}))
    if (!objectsHaveSameKeys(...res)) return false
  }
  return true
}