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

我想比较两个对象,并想制作一个具有共同属性的新对象。我已经看到很多解决方案来采取差异,但不确定我们如何才能采取共同的价值观。

这是我的对象,如果 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))

打字稿版本

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的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>