简体   繁体   English

如何在数组上只分配和迭代一次?

[英]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.我有 2 个不同的 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);

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" } ]; }

You can simply do a for for the players and generate a random number between 0 and the maximum number of avatars, then check the gender and if it coincides with that of the player then assign it, once this is done add the avatar id in a another array and before assigning the next avatar check that the id of the avatars in not in the array just created您可以简单地为玩家做一个 for 并生成一个介于 0 和最大头像数量之间的随机数,然后检查性别,如果它与玩家的性别一致,然后分配它,一旦完成,将头像 id 添加到另一个数组,在分配下一个头像之前,检查头像的 id 是否不在刚刚创建的数组中

Or you can do a simply merge of the 2 array或者您可以简单地合并 2 个数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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