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: [ '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: 'ประเทศไทย'
}
]
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)
}
}
}
})
})
Kindly help me with his.Thanks in advance!!!
CodePudding user response:
Hi there is a simple method for that I use a lot
function arr_intersection(a1: number[], a2: number[]) {
return a1.filter((x) => a2.includes(x));
}
you can also use the intersection method in lodash
const _ = require("lodash");
// Original array
let array1 = [1, 2, 4, 3, 4, 4]
let array2 = [2, 4, 5, 6]
let array3 = [2, 3, 5, 6]
// Using _.intersection() method
let newArray = _.intersection(
array1, array2, array3);
// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
console.log("original Array3: ", array3)
// Printing the newArray
console.log("new Array: ", newArray)
here you can send multiple array not like my method
CodePudding user response:
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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>