Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
How to match different arrays by property and push nested property from one array into another array

So, I have two different arrays, and I need to find a pair match by name, but there is info in the second array I need in the filtered array:

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();

Then I'm filtering the dancers' list out by common name:

    let allDancers = dancers.filter(duet => fighers.some(match => dancers.name === fighers.name)); 

This is great, because I have all the data from the dancers who are also fighers, but I need the info from "class" at the end of their entry in the new filtered array, but since it's named something different, I have to fetch it from the first array and append it to each matching array in the filtered array, but I'm really getting mixed up and I can't seem to find anyone with a similar question on stack overflow.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If the aim is dancers who are also fighters, augmented by matching fighter data, then filter using a lookup and 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)

In case the aim is all dancers, some of them augmented when a matching fighter is found, then it's nearly the same logic, with map instead of filter...

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)

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Create a Map out of the fighters array.

  • Filter the dancers who are also fighters using array using the Map using Array.prototype.filter.

  • Finally map over the filtered array to append the class property.

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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda