get intersection between two objects javascript code example

Example 1: 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));

Example 2: Intersection of two deep objects in JavaScript

const company = {
    lastYearRevenue: 4444,
    capacityInstalled: 22,
    ppaDemands: [ 'f6202f82501ee7350cc73fd', '5f6211ef0b5983761f4d0bb8' ],
    activeProjects: [
      '5f6204162501ee7350cc7406',
      'f6207462501ee7350cc7408',
      '5f62411e21a66a8a5ca6d51f'
    ],
    companyGroup: 'kjkj',
    invoicingAddress: '',
    businessAddress: 'jk',
    legalEntityVatNumber: 'kjkj',
    legalEntity: 'jkj',
    balancingPartyBiddingZones: [
      {
        key: 'balancingPartyBiddingZones',
        value: 'Albania',
        label: 'Albania'
      },
      {
        key: 'balancingPartyBiddingZones',
        value: 'Barbados',
        label: 'Barbados'
      }
    ],
    tradingLicenseBiddingZones: [
      {
        key: 'tradingLicenseBiddingZones',
        value: 'Albania',
        label: 'Albania'
      },
      {
        key: 'tradingLicenseBiddingZones',
        value: 'Barbados',
        label: 'Barbados'
      }
    ],
    isABalancingParty: 'Yes',
    hasTradingLicense: 'Yes',
    ableToProvideFirming: 'Yes',
    leaveNamingRights: 'Yes',
    provideAnotherStructure: '',
    tradingLicense: '1111',
    requireNamingRights: 'Yes',
    signedPpaBefore: 'No',
    parentCompanyGuarantee: 'No',
    offerInvestmentGrade: 'Yes',
    structureProposed: '',
    regionPresent: '',
    creditRating: ' 1111',
    acceptSellersTerms: '',
    historyOfPpa: [],
    favourites: [],
    cleanBuyersAssociation: 'RE100',
    linkToCompany: '',
    signedPpaMarketing: '',
    acceptBuyersTerms: '',
    twoYearPipeline: 22,
    willingToAggregate: 'Yes',
    targetReached: '2034',
    amountOfEnergyBuying: '',
    procurementTarget: '98',
    locations: [],
    energy: [],
    sector: 'Agriculture',
    companyName: '',
    users: [ '5e6f76572097e12186c46e4b' ],
    profileType: '',
    email: '',
    userName: ''
  }

const payload =   { cleanBuyersAssociation: 'RE00' } 

  const getDifference = (a, b) => {
    const entries = Object.entries(b)
    const filteredEntries = Object.entries(b).filter(([key, val]) => key in a )
    console.log(entries)
    console.log('Object.fromEntries(entries) = ', Object.fromEntries(entries))
    console.log('filteredEntries = ', filteredEntries)
    console.log('Object.fromEntries(filteredEntries) = ', Object.fromEntries(filteredEntries))
    // Object.entries(b)
    return Object.fromEntries(Object.entries(b).filter(([key, val]) => {
      return key in a
      // && a[key] !== val
    }))
  }
  
   const toReturn = getDifference(payload, company)

console.log('toReturn: ', toReturn)

    const entries = Object.entries(company)
    console.log(entries)

getDifference(payload, company)