JavaScript:深度检查对象具有相同的键

标签 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,如果:

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

因为 objThree.a.c.fobjThree 中是 undefined

像这样的函数:

'使用严格';

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

最佳答案

如果属性的值是对象,我会进行递归检查。

这里有一个有趣的问题;实际上,有(至少)两个:

  • 如果其中一个“对象”为 null 而另一个没有属性怎么办? true 还是 false
  • 如果其中一个对象具有 {a: null} 而另一个对象具有 {a: 17} 怎么办? true 还是 false
  • 如果其中一个对象具有 {a: null} 而另一个对象具有 {a: {}} 怎么办? true 还是 false

为了本示例的目的,我将 null 视为没有属性的对象,但它非常很大程度上取决于您的用例。我至少可以想到另外两种方法(null 除了 null 之外什么都不匹配,或者 null 除了什么都不匹配一个非对象,即使该对象没有自己的属性)并且可能还有其他属性。

查看评论:

const deepSameKeys = (o1, o2) => {
    // Both nulls = same
    if (o1 === null && o2 === null) {
        return true;
    }

    // Get the keys of each object
    const o1keys = o1 === null ? new Set() : new Set(Object.keys(o1));
    const o2keys = o2 === null ? new Set() : new Set(Object.keys(o2));
    if (o1keys.size !== o2keys.size) {
        // Different number of own properties = not the same
        return false;
    }

    // Look for differences, recursing as necessary
    for (const key of o1keys) {
        if (!o2keys.has(key)) {
            // Different keys
            return false;
        }
        
        // Get the values and their types
        const v1 = o1[key];
        const v2 = o2[key];
        const t1 = typeof v1;
        const t2 = typeof v2;
        if (t1 === "object") {
            if (t2 === "object" && !deepSameKeys(v1, v2)) {
                return false;
            }
        } else if (t2 === "object") {
            // We know `v1` isn't an object
            return false;
        }
    }

    // No differences found
    return true;
};

// Checking your example
const objOne   = {"a": "one",  "b": "two",  "c": {"f": "three_one"}};
const objTwo   = {"a": "four", "b": "five", "c": {"f": "six_one"}};
const objThree = {"a": "four", "b": "five", "c": {"g": "six_one"}};

console.log("objOne vs. objTwo:         ", deepSameKeys(objOne, objTwo));        // true
console.log("objTwo vs. objThree:       ", deepSameKeys(objTwo, objThree));      // false

// `null` checks
console.log("{a: null} vs. {a: 17}      ", deepSameKeys({a: null}, {a: 17}));    // true
console.log("{a: null} vs. {a: {}}      ", deepSameKeys({a: null}, {a: {}}));    // true -- depending on your use case, you may want this to be false
console.log("{a: null} vs. {a: {x:1}}   ", deepSameKeys({a: null}, {a: {x:1}})); // false

// Differing value type check
console.log("{a: 1} vs. {a: '1'}}       ", deepSameKeys({a: 1}, {a: '1'}));      // true

关于JavaScript:深度检查对象具有相同的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41802259/

相关文章:

javascript - URL 匹配正则表达式并不总是有效

javascript - 不规则形状图像映射 - 无 Canvas 、SVG 或 Javascript

javascript - 从内部函数javascript内部分配外部函数的对象

java - 通过 YML 文件将 map 中的符号和特殊字符作为键

Javascript:如何检查对象中是否存在值

javascript - Jquery 模态/对话框表单不起作用

java - 链接 HashMap 按值查找键?

android - 如何在 Mac 中为 facebook SDK 生成 key 哈希

javascript - 在 NodeJS 中创建对象的推荐方法是什么?

javascript - 启用类范围全局变量(Javascript)的潜在危险?