I’m working on a project where I need to compare one array of objects against another. One of the arrays may or may not contain data for each object. It depends on the input, so I need to be able to dynamically compare the two. The first array contains user information:
const users = [
{
firstName: "Jane",
personal: {
data: ["single"],
otherData: {},
},
professional: {
data: ["bachelors"],
otherData: {years_of_edu:[15-17]},
}
},
{
firstName: "John",
personal: {
data: ["married"],
otherData: {},
},
professional: {
data: ["ged"],
otherData: {years_of_edu:[10-12]},
}
},
{
firstName: "Mike",
personal: {
data: ["single"],
otherData: {},
},
professional: {
data: ["bachelors'"],
otherData: {years_of_edu:[15-17]},
}
},
{
firstName: "Sam",
personal: {
data: ["single"],
otherData: [],
},
professional: {
data: ["ged"],
otherData: {years_of_edu:[10-12]},
}
},
]
The second (the one that changes) has this type of a format:
const input = {
personal:{
data: ["single"],
extra: {}
},
professional: {
data: ["bachelor's"],
extra: {years_of_edu:[10-12]}
}
}
I was able to compare the two and get the correct data, but I don’t think my way of accomplishing this is the most efficient nor the fastest. Can anyone advice me on how to make this more efficient? Thank you!
What I currently have:
const usersData = users.filter(function(x) {
for (let item in input) {
for(let items in input[item].extra) {
if (input[item]?.data?.length > 0) {
if (x[item]?.data.includes(...input[item]?.data) &&
x[item]?.otherData[items][0] >= input[item]?.extra[items][0] &&
x[item]?.otherData[items][1] <= input[item]?.extra[items][1]
) {
return x
}
}
}
}
}
)