Entonces, tengo dos matrices diferentes y necesito encontrar una coincidencia de par por nombre, pero hay información en la segunda matriz que necesito en la matriz filtrada:
var dancers = [{ name: "Jim/Pam", //< ---Here's the matching variable I want style: "Salsa", country: "Spain", tier: "Elite", }, { name: "Richard/Enrique", style: "Meringue", country: "Norway", tier: "Amateur", } ]; var fighters = [{ name: "Jim/Pam", //< ---here they are again. They're so talented! style: "KungFu", country: "Spain", class: "Mature (40 - 50)" }, { name: "Bill/Rob", style: "Kickboxing", country: "China", class: "Mature (40 - 50)" }, ]; async function checkDancers() { dancers.forEach(duet => { var dancersInfo = { "name": duet.person0.firstName + "/" + duet.person1.firstName, "style": duet.style, "country": duet.country, "tier": duet.tier, }; dancers.push(dancersInfo); return dancers; }); // console.log(uninames); return dancers; }; async function checkFighters(){ fighters.forEach(match => { var fighersInfo = { "name": match.person0.firstName + "/" + duet.person1.firstName, "style": match.style, "country": match.country, "class": match.class }; sushipairs.push(sushiResult); return sushipairs; }); // console.log(sushitickers); return sushipairs; }; async function match(); var dancers = await checkDancers(); var fighers = await checkFighters();Luego estoy filtrando la lista de bailarines por nombre común:
let allDancers = dancers.filter(duet => fighers.some(match => dancers.name === fighers.name));Esto es genial, porque tengo todos los datos de los bailarines que también son luchadores, pero necesito la información de "clase" al final de su entrada en la nueva matriz filtrada, pero como tiene un nombre diferente, tengo que ir a buscar desde la primera matriz y agréguela a cada matriz coincidente en la matriz filtrada, pero realmente me estoy confundiendo y parece que no puedo encontrar a nadie con una pregunta similar sobre el desbordamiento de pila.
Si el objetivo son bailarines que también son luchadores, aumentados mediante la coincidencia de datos de luchadores, entonces filtre usando una búsqueda y Object.assign() ...
const dancers = [{ name: "Jim/Pam", //< ---Here's the matching variable I want style: "Salsa", country: "Spain", tier: "Elite", }, { name: "Richard/Enrique", style: "Meringue", country: "Norway", tier: "Amateur", } ]; const fighters = [{ name: "Jim/Pam", //< ---here they are again. They're so talented! style: "KungFu", country: "Spain", class: "Mature (40 - 50)" }, { name: "Bill/Rob", style: "Kickboxing", country: "China", class: "Mature (40 - 50)" } ]; const fighterForDancer = d => fighters.find(f => f.name === d.name); const result = dancers.filter(d => { let f = fighterForDancer(d); return f ? Object.assign(d, f) : false; }) console.log(result)En caso de que el objetivo sean todos los bailarines, algunos de ellos aumentados cuando se encuentra un luchador coincidente, entonces es casi la misma lógica, con mapa en lugar de filtro...
const dancers = [{ name: "Jim/Pam", //< ---Here's the matching variable I want style: "Salsa", country: "Spain", tier: "Elite", }, { name: "Richard/Enrique", style: "Meringue", country: "Norway", tier: "Amateur", } ]; const fighters = [{ name: "Jim/Pam", //< ---here they are again. They're so talented! style: "KungFu", country: "Spain", class: "Mature (40 - 50)" }, { name: "Bill/Rob", style: "Kickboxing", country: "China", class: "Mature (40 - 50)" }, ]; const fighterForDancer = d => fighters.find(f => f.name === d.name); const result = dancers.map(d => { let f = fighterForDancer(d) || {}; // or empty object if not found return Object.assign(d, f); }) console.log(result)Crea un mapa a partir de la matriz de fighters .
Filtre a los dancers que también son fighters usando una matriz usando el Map usando Array.prototype.filter .
Finalmente map sobre la matriz filtrada para agregar la propiedad de class .
const dancers = [ { name: "Jim/Pam", style: "Salsa", country: "Spain", tier: "Elite" }, { name: "Richard/Enrique", style: "Meringue", country: "Norway", tier: "Amateur" }, ], fighters = [ { name: "Jim/Pam", style: "KungFu", country: "Spain", class: "Mature (40 - 50)" }, { name: "Bill/Rob", style: "Kickboxing", country: "China", class: "Mature (40 - 50)" }, ], fightersMap = new Map(fighters.map((f) => [f.name, f])), fighterAndDancers = dancers .filter((d) => fightersMap.has(d.name)) .map((d) => ({ ...d, class: fightersMap.get(d.name).class })); console.log(fighterAndDancers);