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

intersection array of object javascript

let data = [
    {
      id: "1",
      name: "test"
    },
    {
      id: "2",
      name: "test"
    }
  ];
  let data2 = [
    {
      id: "1",
      name: "test"
    },
    {
      id: "3",
      name: "test"
    }
  ];
  const result = data.filter((a) => data2.some((b) => a.id === b.id));
  console.log(result);
Comment

PREVIOUS NEXT
Code Example
Javascript :: react-native-popup-menu 
Javascript :: move last element of array to beginning javascript 
Javascript :: wheel 
Javascript :: How to clear one property of state in vuex store 
Javascript :: html show password 
Javascript :: convert json / array to excel in javascript 
Javascript :: input show validation message 
Javascript :: array sort numbers 
Javascript :: lodash remove not in array 
Javascript :: route guard in react js 
Javascript :: react native select option 
Javascript :: how to loop over an array in js 
Javascript :: javascript regex all matches match 
Javascript :: express mysql sessions 
Javascript :: how to use javascript to hide content and show through link 
Javascript :: navigation prompt javascript 
Javascript :: filter object array 
Javascript :: how to get json array response in retrofit 
Javascript :: add icon to angular 
Javascript :: check if an input element has focus 
Javascript :: javascript execute function after async 
Javascript :: MaterialRippleLayout dependency 
Javascript :: jquery xpath 
Javascript :: NodeJS router model 
Javascript :: claim faucets 
Javascript :: Rounding off to desired no of digit after decimal 
Javascript :: how to push array 
Javascript :: table to pdf javascript 
Javascript :: jquery embeded by console 
Javascript :: js NumberFormat 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =