Finding the intersection of properties in Javascript objects

前端 未结 3 1629
野趣味
野趣味 2021-01-28 05:46

Hi guys suppose I have following two objects

var obj1 = {one:232,two:3123,three:3232}
var obj2 = {one:323,three:3444,seven:32}

I am trying writ

3条回答
  •  -上瘾入骨i
    2021-01-28 06:20

    Make it easy on yourself-

    Object.keys returns an array, you can use array filter.

    var commonproperties= function(o1, o2){
        return Object.keys(o1).filter(function(itm){
            return itm in o2
        });
    }
    
    var obj1 = {one:232,two:3123,three:3232},
    obj2 = {one:323,three:3444,seven:32};
    commonproperties(obj1 ,obj2);
    
    /*  returned value: (Array)
    ['one','three']
    */
    

提交回复
热议问题