简体   繁体   中英

How to combine two arrays in javascript?

I have this array:

const participants = [
    {id: 1, name: 'Tom', gender: 'm'}
    ,{id: 2, name: 'Paul', gender: 'm'}
    ,{id: 3, name: 'Igor', gender: 'm'}
    ,{id: 4, name: 'Anna', gender: 'f'}
    ,{id: 5, name: 'Mary', gender: 'f'}
    ,{id: 6, name: 'Alice', gender: 'f'}
];

Now, for each "round" I want to let each man be paired with a girl. Something like:

First round:
Tom and Anna
Paul and Mary
Igor and Alice

Second round:
Tom and Mary
Paul and Alice
Igor and Anna

Thirth round:
Tom and Alice
Paul and Anna
Igor and Mary

However, it could happen that there are only 2 girls (Anna and Mary). In that case, for each round, one of the guy should be "alone".

What is the best way to create an algorithm for that? I have created an algorithm that does not really do the job the right way, as the result is not following the scenario described above.

 const participants = [ { id: 1, name: 'Tom', gender: 'm' }, { id: 2, name: 'Paul', gender: 'm' }, { id: 3, name: 'Igor', gender: 'm' }, { id: 4, name: 'Anna', gender: 'f' }, { id: 5, name: 'Mary', gender: 'f' }, { id: 6, name: 'Alice', gender: 'f' } ]; const men = participants.filter(el => el.gender === 'm'); const women = participants.filter(el => el.gender === 'f'); let currentIndex = 0; let pairs = []; men.forEach((el, index) => { const tmpPair = []; men.forEach((el2, index2) => { let woman = null; if ((index2 + currentIndex) >= women.length) { woman = women[0]; } else { woman = women[index2 + currentIndex]; } tmpPair.push({ man: men[index2], woman: woman }); }); currentIndex++; pairs.push(tmpPair); }); console.log(pairs);

Would this work for you?

const men = participants.filter(el => el.gender === 'm');
const women = participants.filter(el => el.gender === 'f');

var maxlen = Math.max(men.length, women.length)
let pairs = [], pair;

for (var i = 0; i < maxlen; i++) {
    pair={};
     if (men[i] && men[i] != '') pair.man = men[i].name;
     if (women[i] && women[i] != '') pair.woman = women[i].name;
     pairs.push(pair)
}
// pairs is an array like [{"man":"Tom", "woman":"Mary"}, ...]

I changed your sample to get rid of one of the females.

Notice in particular the use of Math.random in the array sorting function. This is assuming your need isn't for 3 rounds in particular, but to create a round on-the-fly.

Secondarily, notice the need to get both the higher of the length of males and females before you iterate. It's tempting to take advantage of the often overlooked index parameter in Array.map to write this out in fully fluent syntax. But I thought it best to loop the old-fashioned way so that you also cover the need of too few men.

 const participants = [ {id: 1, name: 'Tom', gender: 'm'}, {id: 2, name: 'Paul', gender: 'm'}, {id: 3, name: 'Igor', gender: 'm'}, {id: 4, name: 'Anna', gender: 'f'}, {id: 5, name: 'Mary', gender: 'f'} ]; let getGender = (gender) => participants.filter(p => p.gender == gender).sort((a,b) => Math.random() > 0.5? 1: -1); let males = getGender('m'); let females = getGender('f'); let pairs = []; let n = Math.max(males.length, females.length); for(let i = 0; i < n; i++) pairs.push({ man: males[i], woman: females[i] }); for(let pair of pairs) console.log( pairs.man && pairs.woman? `${pair.man.name} and ${pair.woman.name}`: `${pair.man.name || pair.woman.name} is alone` );

Does this works for you?

 const participants = [{ id: 1, name: 'Tom', gender: 'm' }, { id: 2, name: 'Paul', gender: 'm' }, { id: 3, name: 'Igor', gender: 'm' }, { id: 4, name: 'Anna', gender: 'f' }, { id: 5, name: 'Mary', gender: 'f' } ]; const men = participants.filter(p => p.gender === 'm'); const women = participants.filter(p => p.gender == 'f'); const round = men.length; for (i = 0; i < round; i += 1) { console.log(`Round ${i + 1}`); men.forEach((m, index) => { const wIndex = (index + i) % round; const woman = women[wIndex]? ` and ${women[wIndex].name}`: ''; console.log(`${m.name}${woman}`); }) console.log(' '); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM