最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Compare two objects and get common values JavaScript - Stack Overflow

programmeradmin1浏览0评论

I would like to pare two objects and want to make one new object which have mon proprty of it. I have seen lot of solutions to take difference but not sure how we can take mon values.

Here is my objects where if obj1 have ismisionAccount false and obj2 have ismisonAccount true then my result should not have value as it is not matched if both have same property then only will get result.

I know we can use filter if we have arrays of objects but what if we have only objects.

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

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

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

diffrent examples:

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

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

result = {
   "stream":{
   }
}

or

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

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

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

I would like to pare two objects and want to make one new object which have mon proprty of it. I have seen lot of solutions to take difference but not sure how we can take mon values.

Here is my objects where if obj1 have ismisionAccount false and obj2 have ismisonAccount true then my result should not have value as it is not matched if both have same property then only will get result.

I know we can use filter if we have arrays of objects but what if we have only objects.

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

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

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

diffrent examples:

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

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

result = {
   "stream":{
   }
}

or

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

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

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

Share Improve this question edited Dec 8, 2020 at 21:22 app asked Dec 8, 2020 at 21:15 appapp 20711 silver badges31 bronze badges 2
  • 1 I found solution form here enter link description here – idersw Commented Dec 8, 2020 at 21:37
  • it seems getting difference then the mon values. – app Commented Dec 8, 2020 at 21:41
Add a ment  | 

2 Answers 2

Reset to default 5

Something like this should work. A recursive function that just runs through all the keys of the object.

It doesn't handle arrays, but it can be modified to if that's a requirement.

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": {
        "ismisonAccount": false,
        "istradeAccount": true
    }
}

const obj2 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": true
    }
}

const obj3 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": false,
        "istradeAccount": true
    }
}

const obj4 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": false
    }
}

const obj5 = {
    "val1":"test",
    "stream":{
       "ismisonAccount":true,
       "istradeAccount":true
    }
 }
 
 const obj6 = {
    "val1":"test",
    "stream":{
       "ismisonAccount":true,
       "istradeAccount":true
    }
 }

console.log(findCommonValues(obj1, obj2))
console.log(findCommonValues(obj3, obj4))
console.log(findCommonValues(obj5, obj6))

If you want it as small as possible. This is really the best I can do.

const monValues = (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]: monValues(obj1[key], obj2[key]) } : result, {});

const obj1 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": false,
        "istradeAccount": true
    }
}

const obj2 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": true
    }
}

const obj3 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": false,
        "istradeAccount": true
    }
}

const obj4 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": false
    }
}

const obj5 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": true
    }
}

const obj6 = {
    "val1": "test",
    "stream": {
        "ismisonAccount": true,
        "istradeAccount": true
    }
}

console.log(monValues(obj1, obj2))
console.log(monValues(obj3, obj4))
console.log(monValues(obj5, obj6))

TypeScript Version

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 monValues = (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]: monValues(obj1[key] as KeyValueObject, obj2[key] as KeyValueObject) }
                : result,
        {}
    );

Here is a simple example that uses a bination of Object.values and Object.keys as arrays that are filtered to produce your prescribed output:

let obj1 = { "val1":"test", "stream":{ "ismisonAccount":false, "istradeAccount":true } }; 
let obj2 = { "val1":"test", "stream":{ "ismisonAccount":true, "istradeAccount":true } };


let obj3 = { "val1":"test", "stream":{ "ismisonAccount":true, "istradeAccount":true } }; 
let obj4 = { "val1":"test", "stream":{ "ismisonAccount":false, "istradeAccount":true } };

let obj5 = { "val1":"test", "stream":{ "ismisonAccount":false, "istradeAccount":false } }; 
let obj6 = { "val1":"test", "stream":{ "ismisonAccount":false, "istradeAccount":false } };

let obj7 = { "val1":"test", "stream":{ "ismisonAccount":true, "istradeAccount":false } }; 
let obj8 = { "val1":"test", "stream":{ "ismisonAccount":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)); //istradeAccount
console.log(objFilter(obj3, obj4)); //istradeAccount
console.log(objFilter(obj5, obj6)); //ismisonAccount && istradeAccount
console.log(objFilter(obj7, obj8)); //ismisonAccount && istradeAccount
console.log(objFilter(obj2, obj7)); //ismisonAccount

发布评论

评论列表(0)

  1. 暂无评论