Finding the intersection of properties in Javascript objects

前端 未结 3 1625
野趣味
野趣味 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条回答
  •  时光说笑
    2021-01-28 06:17

    I made my own since I don't find the one I need. One line intersection

    x={x:1, y:2, z:1};
    y={x:3, z:4, r:5};
    
    intersect = Object.keys(x).filter(c => Object.keys(y).indexOf(c) !== -1).reduce((a,b) => {let b1={}; b1[b]=x[b]; return {...a, ...b1}}, {});
    console.log(intersect);
    

提交回复
热议问题