Home > Back-end >  How to match different arrays by property and push nested property from one array into another array
How to match different arrays by property and push nested property from one array into another array

Time:05-24

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.

CodePudding user response:

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

CodePudding user response:

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)

  • Related