当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《JavaScript:深层检查对象具有相同的密钥》经验,为你挑选了1个好方法。

问题类似于:如何检查两个对象是否具有相同的属性名称集?但只有一个区别

我想查一下:

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.fundefinedobjThree.

像这样的函数:

'使用严格';

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

T.J. Crowder.. 8

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

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



1> T.J. Crowder..:

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

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));
推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有