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:

JavaScript

I have done the below mentioned code to achieve this:

JavaScript

Advertisement

Answer

JavaScript
JavaScript

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.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement