Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
How to assign and iterate only once over array?

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's a good practice, when you hear "random without duplicates", to think "shuffle".

So to assign random, non-repeating, correctly gendered avatars, segregate the avatars by gender, shuffle the male and female avatars, then assign them sequentially to users....

const avatars = getAvatars();  // just to move the data to the bottom of the snippet
const users = getUsers();

const shuffledMales = shuffle(avatars.filter(a => a.gender==="male"));
const shuffledFemales = shuffle(avatars.filter(a => a.gender==="female"));

let maleIndex = 0, femaleIndex = 0;
users.forEach(user => {
  user.avatar = user.gender === "male" ? shuffledMales[maleIndex++] : shuffledFemales[femaleIndex++];
});

console.log(users);

// fisher-yates shuffle, adapted from https://bost.ocks.org/mike/shuffle/
function shuffle(array) {
  let copy = [],
    n = array.length,
    i;
  while (n) {
    let i = Math.floor(Math.random() * array.length);
    if (i in array) {
      copy.push(array[i]);
      delete array[i];
      n--;
    }
  }
  return copy;
}

function getAvatars() {
  return [{
      "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"
    },
  ];
}

function getUsers() {
  return [{
      "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"
    }
  ];
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda