繁体   English   中英

什么是最简单的方法来检查两个对象之间的键是否匹配?

[英]Whats the easiest way to check if keys match between two objects?

假设你有两个具有相当数量嵌套的对象......

const objA = {
  one: {
    a: 10,
    b: 'string1',
    c: {
      d: 10
    }
  },
  two : 'string1'
}

const objB = {
  one: {
    a: 20,
    b: 'string2',
    c: {
      d: 20
    }
  },
  two : 'string2'
}

有没有一种简单的方法来检查它们之间的所有键是否匹配?

*匹配:两个对象在同一嵌套位置具有相同的键,没有额外的键,所以objAobjB匹配

在普通的Javascript中,您可以获取密钥,比较长度并迭代密钥并检查值是否为对象。

 function compareKeys(a, b) { var keysA = Object.keys(a), keysB = Object.keys(b); return keysA.length === keysB.length && keysA.every(k => b.hasOwnProperty(k) && ( a[k] && typeof a[k] === 'object' || b[k] && typeof b[k] === 'object' ? compareKeys(a[k], b[k]) : true)); } const objA = { one: { a: 10, b: 'string1', c: { d: 10 } }, two : 'string1' }, objB = { one: { a: 20, b: 'string2', c: { d: 20 } }, two : 'string2' }, objC = { one: { a: 20, b: 'string2', c: { d: 20, e: false } }, two : 'string2' }; console.log(compareKeys(objA, objB)); console.log(compareKeys(objA, objC)); console.log(compareKeys({ a: {} }, { a: "test" })); console.log(compareKeys({ a: "test" }, { a: {} })); console.log(compareKeys({ a: { b: "new str" } }, { a: "test" })); 

我认为下面的代码应该广泛涵盖所有类型的对象以供解决。

 const doAllKeysMatch = (obj1, obj2) => { if (typeof obj1 != 'object' && typeof obj1 != 'object') { return true; } if (typeof obj1 == 'object' && typeof obj1 == 'object') { // if both are object types compare all keys let obj1Keys = Object.keys(obj1); let obj2Keys = Object.keys(obj2); return (obj1Keys.length == obj2Keys.length) && obj1Keys.every(key => obj2Keys.includes(key) && doAllKeysMatch(obj1[key], obj2[key])) } /* if only one is of object type check if it doesnt have any keys. if empty object is there then return true which means "abc" and {} have same keys */ if ((typeof obj1 == 'object') && (Object.keys(obj1).length < 1)) { return true } return Object.keys(obj2).length < 1; } console.log(doAllKeysMatch("abc", "dfg")) // true console.log(doAllKeysMatch("abc", {})) // true console.log(doAllKeysMatch({a: "test"}, {a: {}})) // true console.log( doAllKeysMatch( { a: 10, b: 'string1', c: { d: 10 }, two : 'string1' }, { a: 10, b: 'string1', c: { d: false }, two : 'string2' } )) // true 

这与Nina Scholz的答案非常相似,只有足够的思想差异使其成为一个有趣的选择:

 const matchingStructure = (a, b, ak = Object.keys(a), bk = Object.keys(b)) => !(typeof a == 'object' && typeof b == 'object') || ( ak.length === bk.length && ak.every(k => k in b) && ak.every(k => matchingStructure(a[k], b[k])) ) const objA = {one: {a: 10, b: "string1", c: {d: 10}}, two: "string1"} const objB = {one: {a: 20, b: "string2", c: {d: 20}}, two: "string2"} const objC = {one: {a: 30, b: "string3", c: {d: 30}, e: true}, two: "string3"} console.log(matchingStructure(objA, objB)) console.log(matchingStructure(objA, objC)) 

它可能在循环结构上失败。 我没想过。

我建议从lodashhttps: //lodash.com/docs/4.17.11#isEqual查看isEqual的实现

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM