Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!