Feel free to flag this question if this is a duplicate and feel free to reword the title if you have better title. I did not know how to word the title. I have 2 different arrays. First array contains a set of users and second array contains a set of avatars. It's important to mention that the array of avatars will always be bigger than users. I need to pair only one avatar per user. Each user should not have the same avatar and each avatar should match the user's gender. How can I achieve this?
avatars = [
{
"id" : 1,
"image" : "maleavatar1",
"gender": "male"
},
{
"id" : 2,
"image" : "maleavatar2",
"gender": "male"
},
{
"id" : 3,
"image" : "maleavatar3",
"gender": "male"
},
{
"id" : 4,
"image" : "femaleavatar1",
"gender": "female"
},
{
"id" : 5,
"image" : "femaleavatar2",
"gender": "female"
},
{
"id" : 6,
"image" : "femaleavatar3",
"gender": "female"
},
{
"id" : 7,
"image" : "femaleavatar4",
"gender": "female"
},
]
users = [
{
"id" : 1,
"name" : "Manila",
"gender": "female"
},
{
"id" : 2,
"name" : "Josy",
"gender": "female"
},
{
"id" : 3,
"name" : "Eliza",
"gender": "female"
},
{
"id" : 4,
"name" : "Martin",
"gender": "male"
},
{
"id" : 5,
"name" : "Mark",
"gender": "male"
},
{
"id" : 6,
"name" : "John",
"gender": "male"
}
]
var count = 0;
var randUser = '';
var randos = [];
var b_s = users.length;
var a_s = avatars.length;
users.forEach(function(user){count++
avatars.forEach(function(avatar){
if(avatar.gender === user.gender){
randUser = {
"name" : user.name,
"avatar" : avatar.image,
"gender" : user.gender
}
//push users
randos.push(randUser);
}
})
})
console.log(randos);