问题类似于:如何检查两个对象是否具有相同的属性名称集?但只有一个区别
我想查一下:
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.f
是undefined
在objThree.
像这样的函数:
'使用严格';
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));
我只是做一个递归检查,如果属性的值是一个对象; 看评论:
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));