javascript - 比较两个对象并获取共同值 JavaScript

标签 javascript arrays typescript object

我想比较两个对象,并想制作一个具有共同属性的新对象。我已经看到了很多解决差异的解决方案,但不确定我们如何才能采取共同的值(value)观。
这是我的对象,如果 obj1 有 iscommisionAccount false 并且 obj2 有 iscommisionAccount true 那么我的结果不应该有值,因为如果两者具有相同的属性,则结果不匹配,那么只会得到结果。
我知道如果我们有对象数组我们可以使用过滤器但是如果我们只有对象怎么办。

obj 1 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":false,
      "istradeAccount":true
   }
}

 obj 2 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":true,
      "istradeAccount":true
   }
}

result = {
   "stream":{
      "istradeAccount":true
   }
}

diffrent examples:

obj 1 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":false,
      "istradeAccount":true
   }
}

 obj 2 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":true,
      "istradeAccount":false
   }
}

result = {
   "stream":{
   }
}

or

obj 1 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":true,
      "istradeAccount":true
   }
}

 obj 2 = {
   "val1":"test",
   "stream":{
      "iscommisonAccount":true,
      "istradeAccount":true
   }
}

result = {
   "stream":{
      "iscommisonAccount":true,
      "istradeAccount":true
   }
}

最佳答案

像这样的事情应该有效。一个递归函数,它只遍历对象的所有键。
它不处理数组,但可以根据需要进行修改。

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

关于javascript - 比较两个对象并获取共同值 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65207029/

相关文章:

javascript - 使用node.js进行客户端服务器通信

javascript - 在 header 中显示 id 属性

arrays - 我们能否在 O(n) 时间内在未排序的数组中找到没有 hashmap 的数组的模式

typescript - 用 ES6 和 Typescript 开 Jest

javascript - 如何在全屏模式下水平滚动?

javascript - gapi 的身份验证弹出窗口立即关闭并失败并出现 401 错误

c - 查找最大数的数组指针

javascript - 如何对 promise 数组的数组执行promise.all,同时保留对这些数组的引用?

javascript - 使用 Angular 和 chartJS 创建圆形条形图

typescript - 为什么 jquery.d.ts TypeScript 定义文件会抛出编译错误?