javascript - How to check if two objects properties match? - Stack Overflow

Lets say there are two objects but one object has property different from the other. Is there a way to

Lets say there are two objects but one object has property different from the other. Is there a way to figure out what properties match?

for example:

var objectOne = {
  boy: "jack",
  girl: "jill"
}


var objectTwo = {
  boy: "john",
  girl: "mary",
  dog: "mo"
}

edit: It should tell me boy and girl property name are found in both the objects.

Lets say there are two objects but one object has property different from the other. Is there a way to figure out what properties match?

for example:

var objectOne = {
  boy: "jack",
  girl: "jill"
}


var objectTwo = {
  boy: "john",
  girl: "mary",
  dog: "mo"
}

edit: It should tell me boy and girl property name are found in both the objects.

Share Improve this question edited Dec 14, 2016 at 2:36 Deke asked Dec 14, 2016 at 2:34 DekeDeke 4,6595 gold badges49 silver badges70 bronze badges 3
  • Your example doesn't show much. What do you mean? – Andrew Li Commented Dec 14, 2016 at 2:35
  • 1 So you want a list of property names that are in both objects, without regard for what their values are? Shall we assume no object nesting? – nnnnnn Commented Dec 14, 2016 at 3:04
  • @ nnnnnn . Yes Exactly. – Deke Commented Dec 14, 2016 at 3:30
Add a ment  | 

6 Answers 6

Reset to default 3
var in_both = [];
for (var key in objectOne) { // simply iterate over the keys in the first object
    if (Object.hasOwnProperty.call(objectTwo, key)) { // and check if the key is in the other object, too
        in_both.push(key);
    }
}

C.f. https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Now, if you want to test if the values are the same, too, than simply add more code to the condition/body of the inner if.

Using Object.keys

Object.keys(objectOne).filter(k => Object.hasOwnProperty.call(objectTwo, k))

You can use Object.keys and use Array.prototype.reduce to loop through once and list out the mon keys - see demo below:

var objectOne={boy:"jack",girl:"jill"};
var objectTwo={boy:"john",girl:"mary",dog:"mo"};

var result = Object.keys(objectOne).reduce(function(p,c){
  if(c in objectTwo)
    p.push(c);
  return p;
},[]);

console.log(result);

If you want to find out which keys match given two objects, you could loop through all of the keys of the objects using a for... in loop. In my function, it will loop through the keys and return an array of all of the matching keys in the two objects.

let objectOne = {
  boy: "jack",
  girl: "jill"
}

let objectTwo = {
  boy: "john",
  girl: "mary",
  dog: "mo"
}

function matchingKeys (obj1, obj2) {
  let matches = [];
  let key1, key2;
  
  for (key1 in obj1) {
    for (key2 in obj2) {
      if ( key1 === key2) {
        matches.push(key1);
      }
    }
  }
  return matches
}

const result = matchingKeys(objectOne, objectTwo);
console.log(result)

Try this on for size:

function pare(obj1, obj2) {
    // get the list of keys for the first object
    var keys = Object.keys(obj1);

    var result = [];

    // check all from the keys in the first object
    // if it exists in the second object, add it to the result
    for (var i = 0; i < keys.length; i++) {
        if (keys[i] in obj2) {
            result.push([keys[i]])
        }           
    }

    return result;
}

This isn't better than some solutions here, but I thought I'd share:

function objectHas(obj, predicate) {
  return JSON.stringify(obj) === JSON.stringify({ ...obj, ...predicate })
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638897a4637565.html

相关推荐

  • javascript - How to check if two objects properties match? - Stack Overflow

    Lets say there are two objects but one object has property different from the other. Is there a way to

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信