如何在数组上分配和迭代一次?
How to assign and iterate only once over array?
如果这是重复的,请随时标记此问题,如果您有更好的标题,请随时改写标题。我不知道标题怎么写。我有 2 个不同的数组。第一个数组包含一组用户,第二个数组包含一组头像。值得一提的是,化身阵列总是比用户大。我需要为每个用户配对 一个 头像。每个用户不应有相同的头像,每个头像应与用户的性别相匹配。我怎样才能做到这一点?
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);
这是一个很好的做法,当您听到“无重复的随机”时,想想“随机播放”。
因此,要分配随机的、不重复的、正确性别化的头像,按性别隔离头像,混洗男性和女性头像,然后按顺序将它们分配给用户....
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"
}
];
}
如果这是重复的,请随时标记此问题,如果您有更好的标题,请随时改写标题。我不知道标题怎么写。我有 2 个不同的数组。第一个数组包含一组用户,第二个数组包含一组头像。值得一提的是,化身阵列总是比用户大。我需要为每个用户配对 一个 头像。每个用户不应有相同的头像,每个头像应与用户的性别相匹配。我怎样才能做到这一点?
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);
这是一个很好的做法,当您听到“无重复的随机”时,想想“随机播放”。
因此,要分配随机的、不重复的、正确性别化的头像,按性别隔离头像,混洗男性和女性头像,然后按顺序将它们分配给用户....
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"
}
];
}