I have two arrays of objects.
One array contains objects representing users. These objects have two properties, partido and categoria.
A second array contains objects representing collections. These objects may have many properties, which also include the properties partido and categoria.
Question 1
My first question is how do I find objects in the second array that are matches. The object in the second array should be a match if it contains the same properties and values as one of the objects from the first array. The other properties can have any value, so long as partido and categoria match.
The result should be an array containing the matching objects from the second array.
Array 1
const userData = [
{ partido: 'Partido 8', categoria: '2'},
{ partido: 'Partido 13', categoria: '3' }
];
Array 2
collectionData = [
{
partido: 'Partido 8',
Equipo_Local: 'Argentina',
Equipo_Visitante: 'Arabia SaudÃ',
categoria_1: '1',
categoria: '2',
Categoria_3: 'No disponible',
},
{
partido: 'Partido 24',
Equipo_Local: 'Argentina',
Equipo_Visitante: 'México',
Categoria_1: 'Disponible',
categoria: '2',
Categoria_3: 'disponible',
},
{
partido: 'Partido 13',
Equipo_Local: 'Polonia',
Equipo_Visitante: 'Argentina',
Categoria_1: 'No disponible',
Categoria_2: 'No disponible',
categoria: '3',
},
];
So far I've attempted something like:
function compararObjetos(userData, collectionData) {
for (let index = 0; index < userData.length; index++) {
const element = userData[index];
let keys = Object.keys(element);
return collectionData.filter(function(obj) {
for (let i = 0; i < keys.length; i++) {
if(!obj.hasOwnProperty(keys[i]) || obj[keys[i]] !== element[keys[i]]) {
return false;
}
}
return true;
})
}
}
Question 2
My follow-up question is that I would like to perform the same comparison to check for matches, but instead of getting matches from the second array, get a list of objects from the first array that resulted in a match. This assumes we can have many objects in our first array and some may not result in matches. We just want to return the objects that have matches. What is a good way to do this?
Question 3
Finally, rather than check for a match based on all the properties of the objects in the first array, what if we only need to check that certain properties match?
For instance, the objects can have many other properties but in our case there are 4 specific keys/names that need to have matching values for between the two objects in order to be considered a match.
In my example, I need to return a list of objects from the first array userData that resulted in matches for the keys partido, categoria_1, categoria_2 and categoria_3.
Here is what I tried so far. I think this should be returning matches for Tania and Vania, but I cannot figure out why it's still returning an empty array:
const userData = [
{ user_name: "Tania", email: "[email protected]", partido: "8", categoria_1: "1", categoria_2: "2", categoria_3: null },
{ user_name: "Jean", email: "[email protected]", partido: "12", categoria_1: "1", categoria_2: null, categoria_3: "3" },
{ user_name: "Tom", email: "[email protected]", partido: "39", categoria_1: "1", categoria_2: null, categoria_3: null },
{ user_name: "Vania", email: "[email protected]", partido: "13", categoria_1: null, categoria_2: null, categoria_3: "3" }
],
collectionData = [
{
partido: "8",
Equipo_Local: "Argentina",
Equipo_Visitante: "Arabia SaudÃ",
categoria_1: "1",
categoria_2: "No",
categoria_3: "No"
},
{
partido: "24",
Equipo_Local: "Argentina",
Equipo_Visitante: "México",
categoria_1: "No",
categoria_2: "2",
categoria_3: "No"
},
{
partido: "13",
Equipo_Local: "Polonia",
Equipo_Visitante: "Argentina",
categoria_1: "No",
categoria_2: "No",
categoria_3: "3"
}
];
function compareObjects(first, second) {
let matches = [];
second.forEach((possibleMatch) => {
const pickedCollection = (({ partido, categoria_1, categoria_2, categoria_3 }) => ({ partido, categoria_1, categoria_2, categoria_3 }))(possibleMatch);
first.forEach((userData) => {
const isMatch = Object.keys(userData).every((key) => {
return userData[key] === pickedCollection[key];
});
if (isMatch && !matches.find((match) => match === userData)) {
matches.push(userData);
}
});
});
return matches;
}
console.log(compareObjects(userData, collectionData));
returndo an exit function (and ignore loop continuation)