比较两个对象数组和新数组中的 return 个匹配值
Compare two array of objects and return matching values in a new array
我有两个对象数组:inputData
和 jobList
。我必须比较对象数组和 return 的 primarySkills 数组,只有那些在 array.My 对象数组中匹配的值如下:
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: 'ประเทศไทย'
}
]
我已经完成了下面提到的代码来实现这一点:
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: 'ประเทศไทย'
}
]
您好,有一个我经常使用的简单方法
function arr_intersection(a1: number[], a2: number[]) {
return a1.filter((x) => a2.includes(x));
}
你也可以使用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)
这里你可以发送多个数组,不像我的方法
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; }
根据 OP 更详细的进一步要求编辑
"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
."
在这种情况下,需要一项任务,其中 jobList
的 forEach
项重新分配它的 primarySkills
属性 值与 属性 与基的特定交集primarySkills
数组取自 inputData
.
提供的解决方案利用 forEach
and its 2nd thisArg
argument 将功能与外部范围 references/dependencies 分离。
根据是否需要离开 jobList
un-mutated/untouched,可能需要额外的映射任务,该任务必须为每个 jobList
项目创建深度克隆。
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; }
像这样更改您的验证
matchingPrimarySkills = []
jobList.forEach((job) => {
inputData.forEach((candidate) => {
job.primarySkills.forEach( (jobPrimarySkill) => {
candidate.primarySkills.forEach( (candidatePrimarySkill) => {
if (jobPrimarySkill === candidatePrimarySkill && matchingPrimarySkills.indexOf(jobPrimarySkill) === -1) {
matchingPrimarySkills.push(jobPrimarySkill)
}
})
})
})
})
// With your examples, it will og [ "javascript"] without duplicates
console.log(matchingPrimarySkills)
这是实现所需输出的一种方法。仅显示相关属性仅供演示。您可以将求职者从 inputData
传递到 getJobs(input)
函数,以获取与输入中的某些 primarySkills
相匹配的职位列表:
let inputData = [
{ candidateID: '111111', primarySkills: ['asp.net', 'react', 'javascript'] },
{ candidateID: '222222', primarySkills: ['python'] },
{ candidateID: '333333', primarySkills: ['powershell', 'node'] },
]
let jobList = [
{ jobId: '77777', primarySkills: ['asp.net', 'node'] },
{ jobId: '88888', primarySkills: ['javascript', 'python'] },
{ jobId: '99999', primarySkills: ['sqlserver', 'azure', 'powershell'] },
]
function getJobs(input) {
// Get a copy of jopList
const temp = jobList.map(obj => { return { ...obj } })
return temp.filter(job => {
// Filter the job's primarySkills to only show
// skills matching the candidates's primarySkills.
job.primarySkills = job.primarySkills.filter(jobSkill =>
input.primarySkills.includes(jobSkill)
)
return job.primarySkills.length
})
}
// Get jobs for all candidates in inputData
for (let candidate of inputData) {
console.log(`\nJobs for candidateID ${candidate.candidateID}:\n`, getJobs(candidate))
}
我有两个对象数组:inputData
和 jobList
。我必须比较对象数组和 return 的 primarySkills 数组,只有那些在 array.My 对象数组中匹配的值如下:
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: 'ประเทศไทย'
}
]
我已经完成了下面提到的代码来实现这一点:
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: 'ประเทศไทย'
}
]
您好,有一个我经常使用的简单方法
function arr_intersection(a1: number[], a2: number[]) {
return a1.filter((x) => a2.includes(x));
}
你也可以使用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)
这里你可以发送多个数组,不像我的方法
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; }
根据 OP 更详细的进一步要求编辑
"I want to compare the
inputData.primarySkills
one by one with each list of objects injobList
and update the matching skills against theprimarySkills
field ofjobList
."
在这种情况下,需要一项任务,其中 jobList
的 forEach
项重新分配它的 primarySkills
属性 值与 属性 与基的特定交集primarySkills
数组取自 inputData
.
提供的解决方案利用 forEach
and its 2nd thisArg
argument 将功能与外部范围 references/dependencies 分离。
根据是否需要离开 jobList
un-mutated/untouched,可能需要额外的映射任务,该任务必须为每个 jobList
项目创建深度克隆。
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; }
像这样更改您的验证
matchingPrimarySkills = []
jobList.forEach((job) => {
inputData.forEach((candidate) => {
job.primarySkills.forEach( (jobPrimarySkill) => {
candidate.primarySkills.forEach( (candidatePrimarySkill) => {
if (jobPrimarySkill === candidatePrimarySkill && matchingPrimarySkills.indexOf(jobPrimarySkill) === -1) {
matchingPrimarySkills.push(jobPrimarySkill)
}
})
})
})
})
// With your examples, it will og [ "javascript"] without duplicates
console.log(matchingPrimarySkills)
这是实现所需输出的一种方法。仅显示相关属性仅供演示。您可以将求职者从 inputData
传递到 getJobs(input)
函数,以获取与输入中的某些 primarySkills
相匹配的职位列表:
let inputData = [
{ candidateID: '111111', primarySkills: ['asp.net', 'react', 'javascript'] },
{ candidateID: '222222', primarySkills: ['python'] },
{ candidateID: '333333', primarySkills: ['powershell', 'node'] },
]
let jobList = [
{ jobId: '77777', primarySkills: ['asp.net', 'node'] },
{ jobId: '88888', primarySkills: ['javascript', 'python'] },
{ jobId: '99999', primarySkills: ['sqlserver', 'azure', 'powershell'] },
]
function getJobs(input) {
// Get a copy of jopList
const temp = jobList.map(obj => { return { ...obj } })
return temp.filter(job => {
// Filter the job's primarySkills to only show
// skills matching the candidates's primarySkills.
job.primarySkills = job.primarySkills.filter(jobSkill =>
input.primarySkills.includes(jobSkill)
)
return job.primarySkills.length
})
}
// Get jobs for all candidates in inputData
for (let candidate of inputData) {
console.log(`\nJobs for candidateID ${candidate.candidateID}:\n`, getJobs(candidate))
}