慕仙森
像这样的东西应该有效。一个递归函数,仅遍历对象的所有键。它不处理数组,但如果需要的话可以对其进行修改。function findCommonValues(obj1, obj2) { var result = {} for (let key in obj1) { if (obj1[key] && obj1[key] === obj2[key]) result[key] = obj1[key] else if (typeof obj1[key] === 'object' && obj1[key] !== null) { result[key] = findCommonValues(obj1[key], obj2[key]) } } return result;}const obj1 = { "val1": "test", "stream": { "iscommisonAccount": false, "istradeAccount": true }}const obj2 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": true }}const obj3 = { "val1": "test", "stream": { "iscommisonAccount": false, "istradeAccount": true }}const obj4 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": false }}const obj5 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } } const obj6 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } }console.log(findCommonValues(obj1, obj2))console.log(findCommonValues(obj3, obj4))console.log(findCommonValues(obj5, obj6))如果你希望它尽可能小。这确实是我能做的最好的事情了。const commonValues = (obj1, obj2) => Object.keys(obj1).reduce((result, key) => obj1[key] && obj1[key] === obj2[key] ? { ...result, [key]: obj1[key] } : typeof obj1[key] === 'object' && obj1[key] !== null ? { ...result, [key]: commonValues(obj1[key], obj2[key]) } : result, {});const obj1 = { "val1": "test", "stream": { "iscommisonAccount": false, "istradeAccount": true }}const obj2 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": true }}const obj3 = { "val1": "test", "stream": { "iscommisonAccount": false, "istradeAccount": true }}const obj4 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": false }}const obj5 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": true }}const obj6 = { "val1": "test", "stream": { "iscommisonAccount": true, "istradeAccount": true }}console.log(commonValues(obj1, obj2))console.log(commonValues(obj3, obj4))console.log(commonValues(obj5, obj6))TypeScript 版本export type KeyValueObject = { [key: string]: number | boolean | string | KeyValueObject}export const isKeyValueObject = (obj1: number | boolean | string | KeyValueObject): obj1 is KeyValueObject => typeof obj1 === 'object' && obj1 !== null;export const commonValues = (obj1: KeyValueObject, obj2: KeyValueObject): KeyValueObject => Object.keys(obj1).reduce((result, key) => obj1[key] && obj1[key] === obj2[key] ? { ...result, [key]: obj1[key] } : isKeyValueObject(obj1[key]) && isKeyValueObject(obj2[key]) ? { ...result, [key]: commonValues(obj1[key] as KeyValueObject, obj2[key] as KeyValueObject) } : result, {} );
慕妹3146593
这是一个简单的示例,它使用 Object.values 和 Object.keys 的组合作为数组,经过过滤以生成指定的输出:let obj1 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } }; let obj2 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } };let obj3 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":true } }; let obj4 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":true } };let obj5 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } }; let obj6 = { "val1":"test", "stream":{ "iscommisonAccount":false, "istradeAccount":false } };let obj7 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } }; let obj8 = { "val1":"test", "stream":{ "iscommisonAccount":true, "istradeAccount":false } };interface Data {stream:{[key: string]: boolean}};function objFilter(objA: Data, objB: Data): Data { let out: Data = {stream:{}}; Object.keys(objA.stream).filter((value, idx) => Object.values(objA.stream)[idx] === Object.values(objB.stream)[idx] ? out.stream[value] = Object.values(objA.stream)[idx] : false ); return out;}console.log(objFilter(obj1, obj2)); //istradeAccountconsole.log(objFilter(obj3, obj4)); //istradeAccountconsole.log(objFilter(obj5, obj6)); //iscommisonAccount && istradeAccountconsole.log(objFilter(obj7, obj8)); //iscommisonAccount && istradeAccountconsole.log(objFilter(obj2, obj7)); //iscommisonAccount