Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find intersection between two object arrays javascript

// Generic helper function that can be used for the three operations:        
const operation = (list1, list2, isUnion = false) =>
    list1.filter(
        (set => a => isUnion === set.has(a.userId))(new Set(list2.map(b => b.userId)))
    );

// Following functions are to be used:
const inBoth = (list1, list2) => operation(list1, list2, true),
      inFirstOnly = operation,
      inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

// Sample data
const list1 = [
    { userId: 1234, userName: 'XYZ'  }, 
    { userId: 1235, userName: 'ABC'  }, 
    { userId: 1236, userName: 'IJKL' },
    { userId: 1237, userName: 'WXYZ' }, 
    { userId: 1238, userName: 'LMNO' }
];
const list2 = [
    { userId: 1235, userName: 'ABC'  },  
    { userId: 1236, userName: 'IJKL' },
    { userId: 1252, userName: 'AAAA' }
];
  
console.log('inBoth:', inBoth(list1, list2)); 
console.log('inFirstOnly:', inFirstOnly(list1, list2)); 
console.log('inSecondOnly:', inSecondOnly(list1, list2));
Comment

intersection of two objects in javascript

var firstObject = {
  x: 0,
  y: 1,
  z: 2,

  a: 10,
  b: 20,
  e: 30
}

var secondObject = {
  x: 0,
  y: 1,
  z: 2,

  a: 10,
  c: 20,
  d: 30
}

function getIntKeys(obj1, obj2){

    var k1 = Object.keys(obj1);
    return k1.filter(function(x){
        return obj2[x] !== undefined;
    });
  
}

alert(getIntKeys(firstObject, secondObject));
Comment

PREVIOUS NEXT
Code Example
Javascript :: js insert a point each three digit 
Javascript :: sending json data uing fetch is empty 
Javascript :: WebPack Multiple files 
Javascript :: use axios cancel token in react.js useEffect 
Javascript :: hash url owl carousel example 
Javascript :: jq click with trigger load data 
Javascript :: array remove duplicates javascript 
Javascript :: npm install say unmet dependencies 
Javascript :: convert svg to react component 
Javascript :: nodejs check if file is running on server or client 
Javascript :: react axios Card List 
Javascript :: getx oninit 
Javascript :: useWidthSize 
Javascript :: react loop return 
Javascript :: convert boolean to string javascript 
Javascript :: react router v6 pass props 
Javascript :: using while loop to create table rows js 
Javascript :: onclick automatically called after 10 seconds 
Javascript :: foreach loop 
Javascript :: how to alert in javascript 
Javascript :: Event Custom Fire 
Javascript :: mule 4 json to string json 
Javascript :: es6 get first and last element of array 
Javascript :: react native on refresh change color flat list 
Javascript :: separador de miles javascript 
Javascript :: get list of filenames in folder 
Javascript :: selectboxit 
Javascript :: .env file example react native 
Javascript :: decrementar en java 
Javascript :: javascript make title blink 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =