intersection of two objects in javascript

Solutions on MaxInterview for intersection of two objects in javascript by the best coders in the world

showing results for - "intersection of two objects in javascript"
Trent
07 Mar 2020
1var firstObject = {
2  x: 0,
3  y: 1,
4  z: 2,
5
6  a: 10,
7  b: 20,
8  e: 30
9}
10
11var secondObject = {
12  x: 0,
13  y: 1,
14  z: 2,
15
16  a: 10,
17  c: 20,
18  d: 30
19}
20
21function getIntKeys(obj1, obj2){
22
23    var k1 = Object.keys(obj1);
24    return k1.filter(function(x){
25        return obj2[x] !== undefined;
26    });
27  
28}
29
30alert(getIntKeys(firstObject, secondObject));
Marco
11 Jan 2020
1// Generic helper function that can be used for the three operations:        
2const operation = (list1, list2, isUnion = false) =>
3    list1.filter(
4        (set => a => isUnion === set.has(a.userId))(new Set(list2.map(b => b.userId)))
5    );
6
7// Following functions are to be used:
8const inBoth = (list1, list2) => operation(list1, list2, true),
9      inFirstOnly = operation,
10      inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);
11
12// Sample data
13const list1 = [
14    { userId: 1234, userName: 'XYZ'  }, 
15    { userId: 1235, userName: 'ABC'  }, 
16    { userId: 1236, userName: 'IJKL' },
17    { userId: 1237, userName: 'WXYZ' }, 
18    { userId: 1238, userName: 'LMNO' }
19];
20const list2 = [
21    { userId: 1235, userName: 'ABC'  },  
22    { userId: 1236, userName: 'IJKL' },
23    { userId: 1252, userName: 'AAAA' }
24];
25  
26console.log('inBoth:', inBoth(list1, list2)); 
27console.log('inFirstOnly:', inFirstOnly(list1, list2)); 
28console.log('inSecondOnly:', inSecondOnly(list1, list2));
queries leading to this page
find intersection of two strings jshow to intersect array in reactfind intersection of two arrays of objects javascriptget only intersection of multiple arrays of objects intersection apply not working in angular 2intersection of two objects in node jsjavascript object intersectioncheck for the intersection between to objects jslodash intersection union minusintersection between array objectintersection two arrays javascript objecthow to find the intersection between two arrays jsfind intersection between two arrays javascriptjavascript intersection of two arrays objectscheck for the intersect between to objects jscan you find intersection of array of objects in javascriptintersection observer javascriptopposite of intersect in lodashobject intersection javascriptget intersection of two array of objects javascriptjs intersection of two objectsintersection object javascriptjavascript intersection of two arraysintersection of two arrays observalbe arraysjavascript find intersection of two array of objectsintersection of two objects in javascriptintersection two json objects javascriptintersection of array of objectsthree js intersection of 2 objectsintersection for array of objects in javascriptintersection of two array of objects javascriptfind a intersection of two arrays jsfind intersection between 2 arrays javascriptintersection of two array of objects javascriptjavascript intersection of two arrays of objectsfind intersection of two arrays javascript objectfind intersection of two arrays javascriptarray of objects intersection javascriptget intersection within object of arraysjavascript intersection of two object arrays javascript get intersection of two arraysget intersection of two arrays javascriptintersection 3 object arrays javascriptfind intersection of 2 arrays javascriptfind intersection of two arrays of objects typescriptintersection of objects in two arrays jsintersection of object javascriptjs get intersection of two objectsjs intersection observerarray intersection objects typescriptjavascript intersection of two arrays string insensitivejs array intersection objectjavascript intersection of two arrays of objects by valuesjs get intersection of two arraysfind intersection between two object arrays javascriptjavascript intersect array of objectsjavascript find intersection of two arraysintersection of two deep objects in javascriptget intersection object from two objects javascriptintersection array of objects javascriptintersection of two arrays of object javascriptintersect 2 similar arrays javascript into object intersection of two arrays of objects javascript outer intersect two arraystypescript get intersection properties on two objectsfind intersection between two arrays of objects javascriptget intersection elements from two or more arrays javascriptget intersection between two objects javascriptintersection array of objects jsintersection of two arrays containing objectsintersection of two objects array object intersection by id javascriptcompute intersection of two object arrays in javascriptintersection of two objects javascriptfind intersection of two arrays elements javascriptintersection of two object arrays javascript by idjavascript intersection of two array of objectsget only intersection of all arrays of objectsintersect two objects javascriptjavascript find intersection of two objectsthree js intersection of 2 objecthow to find intersection and merge objects of 2 arrays in javascriptintersection of two object arrays javascriptarray of object intersection jsjs array intersect callbackintersection of two arrays of objects javascriptarray intersection angularintersection of two objects in javascript