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

underscore.js - Find the common members of two Javascript objects - Stack Overflow

programmeradmin4浏览0评论

What is the easiest way to find the common members in two Javascript objects? This question is not about equality. I don't care about the values of each member, just that they exist in both objects.

Here's what I've done so far (using underscore.js):

_.intersection(_.keys({ firstName: 'John' }), _.keys({ firstName: 'Jane', lastName: 'Doe' }))

This gives me a result of ['firstName'] as expected, but I would like to find an easier or more efficient way, preferably vanilla Javascript.

  • Is there a better/easier way to do this with underscore?
  • Is there a better/easier way to do this without underscore (preferred)?

What is the easiest way to find the common members in two Javascript objects? This question is not about equality. I don't care about the values of each member, just that they exist in both objects.

Here's what I've done so far (using underscore.js):

_.intersection(_.keys({ firstName: 'John' }), _.keys({ firstName: 'Jane', lastName: 'Doe' }))

This gives me a result of ['firstName'] as expected, but I would like to find an easier or more efficient way, preferably vanilla Javascript.

  • Is there a better/easier way to do this with underscore?
  • Is there a better/easier way to do this without underscore (preferred)?
Share Improve this question edited Jun 3, 2013 at 1:15 dda 6,2032 gold badges27 silver badges35 bronze badges asked Jun 3, 2013 at 0:43 sellmeadogsellmeadog 7,5171 gold badge33 silver badges45 bronze badges 2
  • 3 you probably meant _.intersecton – go-oleg Commented Jun 3, 2013 at 0:46
  • 1 In plain JS you could use ES5s Object.keys on each object and pick the one with the least keys to check against the other, falling back to a for..in loop and hasOwnProperty test for older user agents. – RobG Commented Jun 3, 2013 at 0:50
Add a comment  | 

4 Answers 4

Reset to default 12

Sure, just iterate through the keys of one object and construct an array of the keys that the other object shares:

function commonKeys(obj1, obj2) {
  var keys = [];
  for(var i in obj1) {
    if(i in obj2) {
      keys.push(i);
    }
  }
  return keys;
}

This will work for modern browsers:

function commonKeys(a, b) {
    return Object.keys(a).filter(function (key) { 
        return b.hasOwnProperty(key); 
    });
};

// ["firstName"]
commonKeys({ firstName: 'John' }, { firstName: 'Jane', lastName: 'Doe' });
var common = [];
for (var key in obj2) if (key in obj1) common.push(key);

Edit for RobG: If you happen to be working in an environment that includes code that is not your own, and you do not trust the author(s) to have extended Object.prototype correctly, then you might want to do:

var common = [];
for (var k in obj2) if (obj2.hasOwnProperty(k) && obj1.hasOwnProperty(k)) common.push(k);

However, as I have stated in the comments below, I have written an article (with an inflammatory title) about why I believe that this used to be good advice, but is good advice no longer:
http://phrogz.net/death-to-hasownproperty

I know this question has already been answered but I wanted to offer an "underscore" way of doing it. I am also hoping that people will discuss the best "underscore" way of doing it here too.

var user1 = { firstName: 'John' }, user2 = { firstName: 'Jane', lastName: 'Doe' };

_.keys(_.pick(user1, _.keys(user2)))

that is my best shot at it, it doesn't reduct the underscore primitives from the original question so maybe this is the best you can do.

Here is the original question in my format for reference

_.intersection(_.keys(user1), _.keys(user2))
发布评论

评论列表(0)

  1. 暂无评论