Skip to content
Advertisement

Compare two array of objects and return matching values in a new array

I have two arrays of objects: inputData and jobList. I have to compare the primarySkills array of both the array of objects and return only those values which are matching in both the array.My array of objects are as below:

let inputData = [
    {
        "candidateID": "911772331",
        "skillSet": ["Information Technology"],
        "addressCity": "Bengaluru",
        "addressState": "KA",
        "country": "India",
        "primarySkills": ['asp.net', 'react', 'javascript'],
        "secondarySkills": ['powerbi', 'redux'],
        "preferredPositionType": [],
    }
]

let jobList = [
  {
    jobId: '600039355',
    jobType: 'fulltime',
    primarySkills: [ 'asp.net','node' ],
    secondarySkills: [ 'javascript' ],
    skillSet: [ 'javascript' ],
    Address: 'Indonesia, Bekasi Kabupaten, 53, Jalan Londan 5',
    City: 'Bekasi Kabupaten',
    State: 'JABODETABEK',
    Zipcode: '17522',
    Country: 'Indonesia'
  },
  {
    jobId: '562190375',
    jobType: 'fulltime',
    primarySkills: [ 'javascript','mainframe' ],
    secondarySkills: [ 'javascript' ],
    skillSet: [ 'javascript' ],
    Address: 'India, Pune, 411001, Pune, Pune Station',
    City: 'Pune',
    State: 'MH',
    Zipcode: '411001',
    Country: 'India'
  },
  {
    jobId: '883826845',
    jobType: 'fulltime',
    primarySkills: [ 'sqlserver', 'react', 'powershell' ],
    secondarySkills: [ 'powerbi' ],
    skillSet: [ 'powerbi' ],
    Address: 'ประเทศไทย, หมู่ที่ 3, 1234',
    City: 'หมู่ที่ 3',
    State: null,
    Zipcode: '57110',
    Country: 'ประเทศไทย'
  }
]

I have done the below mentioned code to achieve this:

jobList.forEach((item) => {
  inputData.forEach((data) => {
    for (let i = 0; i <= item.primarySkills.length; i++) {
      for (let j = 0; j <= data.primarySkills.length; j++) {
        if (item.primarySkills[i] === data.primarySkills[j]) {
          PMSkill.push(item.primarySkills[i]);
        } else {                                    
          PMSkill.push(0)
        }
      }
    }
  })
})
Expected output to be like in the PMSkill array:
let PMSkill= [
  {
    jobId: '600039355',
    jobType: 'fulltime',
    primarySkills: [ 'asp.net'],----here asp.net is the only skill matching with inputData primarySkill
    secondarySkills: [ 'javascript' ],
    skillSet: [ 'javascript' ],
    Address: 'Indonesia, Bekasi Kabupaten, 53, Jalan Londan 5',
    City: 'Bekasi Kabupaten',
    State: 'JABODETABEK',
    Zipcode: '17522',
    Country: 'Indonesia'
  },
  {
    jobId: '562190375',
    jobType: 'fulltime',
    primarySkills: [ 'javascript'],
    secondarySkills: [ 'javascript' ],
    skillSet: [ 'javascript' ],
    Address: 'India, Pune, 411001, Pune, Pune Station',
    City: 'Pune',
    State: 'MH',
    Zipcode: '411001',
    Country: 'India'
  },
  {
    jobId: '883826845',
    jobType: 'fulltime',
    primarySkills: ['react'],
    secondarySkills: [ 'powerbi' ],
    skillSet: [ 'powerbi' ],
    Address: 'ประเทศไทย, หมู่ที่ 3, 1234',
    City: 'หมู่ที่ 3',
    State: null,
    Zipcode: '57110',
    Country: 'ประเทศไทย'
  }
]

Advertisement

Answer

function getIntersection(x, y) {
  // ensure two arrays ...
  const [
    comparisonBase, // ... the shorter one as comparison base
    comparisonList, // ... the longer one to filter from.
  ] = [[...x], [...y]]
    .sort((a, b) => a.length - b.length);

  // create a `Map` based lookup table from the shorter array.
  const itemLookup = comparisonBase
    .reduce((map, item) => map.set(item, true), new Map)

  // the intersection is the result of following filter task.
  return comparisonList.filter(item => itemLookup.has(item));
}

const inputData = [{
  "candidateID": "911772331",
  "skillSet": ["Information Technology"],
  "addressCity": "Bengaluru",
  "addressState": "KA",
  "country": "India",
  "primarySkills": ['asp.net', 'react', 'javascript'],
  "secondarySkills": ['powerbi', 'redux'],
  "preferredPositionType": [],
}];
const jobList = [{
  jobId: '600039355',
  jobType: 'fulltime',
  primarySkills: [ 'javascript' ],
  secondarySkills: [ 'javascript' ],
  skillSet: [ 'javascript' ],
  Address: 'Indonesia, Bekasi Kabupaten, 53, Jalan Londan 5',
  City: 'Bekasi Kabupaten',
  State: 'JABODETABEK',
  Zipcode: '17522',
  Country: 'Indonesia'
}, {
  jobId: '562190375',
  jobType: 'fulltime',
  primarySkills: [ 'javascript' ],
  secondarySkills: [ 'javascript' ],
  skillSet: [ 'javascript' ],
  Address: 'India, Pune, 411001, Pune, Pune Station',
  City: 'Pune',
  State: 'MH',
  Zipcode: '411001',
  Country: 'India'
}, {
  jobId: '883826845',
  jobType: 'fulltime',
  primarySkills: [ 'sqlserver', 'azure', 'powershell' ],
  secondarySkills: [ 'powerbi' ],
  skillSet: [ 'powerbi' ],
  Address: 'ประเทศไทย, หมู่ที่ 3, 1234',
  City: 'หมู่ที่ 3',
  State: null,
  Zipcode: '57110',
  Country: 'ประเทศไทย'
}];

const candidateSkills = inputData[0].primarySkills;
const openJobsSkills = [...new Set(
  jobList.reduce((arr, { primarySkills }) => arr.concat(primarySkills), [])
)];

const skillIntersection = getIntersection(openJobsSkills, candidateSkills);

console.log({ candidateSkills, openJobsSkills, skillIntersection });
.as-console-wrapper { min-height: 100%!important; top: 0; }

Edit according to the OP’s more detailed further request

“I want to compare the inputData.primarySkills one by one with each list of objects in jobList and update the matching skills against the primarySkills field of jobList.”

In this case one needs a task which forEach item of jobList reassigns it’s primarySkills property value with the property specific intersection with the base primarySkills array taken from inputData.

The provided solution makes use of forEach and its 2nd thisArg argument in order to decouple the functionality from outside scope references/dependencies.

Depending on whether one needs to leave jobList un-mutated/untouched, on might be in need of an additional mapping task which has to create a deep clone of each jobList item.

function getIntersection(x, y) {
  // ensure two arrays ...
  const [
    comparisonBase, // ... the shorter one as comparison base
    comparisonList, // ... the longer one to filter from.
  ] = [[...x], [...y]]
    .sort((a, b) => a.length - b.length);

  // create a `Map` based lookup table from the shorter array.
  const itemLookup = comparisonBase
    .reduce((map, item) => map.set(item, true), new Map)

  // the intersection is the result of following filter task.
  return comparisonList.filter(item => itemLookup.has(item));
}

const inputData = [{
  "candidateID": "911772331",
  "skillSet": ["Information Technology"],
  "addressCity": "Bengaluru",
  "addressState": "KA",
  "country": "India",
  "primarySkills": ['asp.net', 'react', 'javascript'],
  "secondarySkills": ['powerbi', 'redux'],
  "preferredPositionType": [],
}];

const jobList = [{
  jobId: '600039355',
  jobType: 'fulltime',
  primarySkills: [ 'asp.net','node' ],
  secondarySkills: [ 'javascript' ],
  skillSet: [ 'javascript' ],
  Address: 'Indonesia, Bekasi Kabupaten, 53, Jalan Londan 5',
  City: 'Bekasi Kabupaten',
  State: 'JABODETABEK',
  Zipcode: '17522',
  Country: 'Indonesia'
}, {
  jobId: '562190375',
  jobType: 'fulltime',
  primarySkills: [ 'javascript','mainframe' ],
  secondarySkills: [ 'javascript' ],
  skillSet: [ 'javascript' ],
  Address: 'India, Pune, 411001, Pune, Pune Station',
  City: 'Pune',
  State: 'MH',
  Zipcode: '411001',
  Country: 'India'
}, {
  jobId: '883826845',
  jobType: 'fulltime',
  primarySkills: [ 'sqlserver', 'react', 'powershell' ],
  secondarySkills: [ 'powerbi' ],
  skillSet: [ 'powerbi' ],
  Address: 'ประเทศไทย, หมู่ที่ 3, 1234',
  City: 'หมู่ที่ 3',
  State: null,
  Zipcode: '57110',
  Country: 'ประเทศไทย'
}];

function updatePrimarySkillsWithIntersectionOfBoundBaseSkills(jobItem) {
  const basePrimarySkills = this;

  jobItem.primarySkills =
    getIntersection(jobItem.primarySkills, basePrimarySkills);
}

// if needed ... create new array with real `jobItem` clones ...
const pmSkillList = jobList.map(jobItem =>
  JSON.parse(JSON.stringify(jobItem))
);
// ... otherwise (`jobList.forEach`) ... just ...

// .... reassign the item specific `primarySkills` property value.
pmSkillList.forEach(
  // the callback
  updatePrimarySkillsWithIntersectionOfBoundBaseSkills,
  // the 2nd `thisArg` argument
  inputData[0].primarySkills,
);

// log any involved data.
console.log({ inputData, jobList, pmSkillList });
.as-console-wrapper { min-height: 100%!important; top: 0; }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement