如何最好地分组和呈现项目,例如使用在许多"User"实例中可能相等的"interest"属性?


大家好,我在制作函数时遇到了一些问题。如代码中所示。我想做interestMatch函数。该函数需要做的是查看所有的用户,并在我的代码中找到那些有相同兴趣的用户——bob和jack。我认为它应该是一个if语句。金达:";如果任何用户具有相同的兴趣"返回此用户,此用户匹配"有人能帮我吗!?非常感谢!
class User {
constructor(username, password, firstname, lastname, email, birthday, gender, interest){
this.username = username;
this.password = password;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.birthday = birthday;
this.gender = gender;
this.interest = interest
}
}
let InterrestArray = ["Netflix", "Sports", "Party", "Business", "Children", "Hygge"]
let bob = new User ("bob123", "12345", "Bob", "bobiski", "bob@bob.com", "2000-10-10", "male", interrest[0]);
let jack = new User ("jack20", "340302", "Jack", "jackiski", "jack@jack.com", "2000-06-10", "male", interrest[0]);
let thif = new User ("thif20", "204903", "Thifanny", "Thifiski", "thif@jack.com", "2000-09-12", "famale", interrest[1]);
function interestMatch(){
???
}
console.log(interestMatch())
// I want it to console.log - "bob maches with jack!" - because the have the same interrest
```

一个非常简单的方法是有两个嵌套的for循环,它们迭代用户并比较他们的兴趣:

function interestMatch(users){
for (let i = 0; i < users.length; i++) {
const user1 = users[i];
// No need to iterate over any user that comes "before" user1.
for (let j = i+1; j < users.length; j++) {
const user2 = users[j];
if (user1.interest === user2.interest) {
return `${user1.username} matches with ${user2.username}`;
}
}
}
}

当然,如果用户可以有多个兴趣,或者你想返回所有匹配项,事情会变得更复杂。在这种情况下,创建interest -> list of users映射可能是有意义的。

OP正在寻找的任何方法都比复杂一点

它应该是带有if语句的东西

一种有效的方法是扫描用户列表,查找每个用户的兴趣,并将用户推入其兴趣相关的数组。因此,基本方法创建具有兴趣特定用户列表的基于interest的地图或索引。

这只是第一部分,提供了一种创建可以进一步处理的数据结构的方法。

有了所需的数据结构(键值存储,其中interest是键,特定的用户列表是每个键的值(,我们就可以接近第二部分,即关于输出/渲染。

对于每个键值对(兴趣和用户列表(,都会调用特定于用户列表的渲染函数。在该步骤中,还将user列表映射到例如每个用户的名字的列表中。

最后的渲染函数需要处理边缘情况。。。喜欢有一个用户,其兴趣是其他人无法分享的。。。或有两个以上的用户有相同的兴趣。。。

class User {
constructor(
username, password, firstname, lastname,
email, birthday, gender, interest
) {
Object.assign(this, {
username, password, firstname, lastname,
email, birthday, gender, interest
});
}
}
function groupUserByInterest(index, user) {
const { interest } = user;
const sameInterestList = index[interest] || (index[interest] = []);
sameInterestList.push(user);
return index;
}
const interestList = ["Netflix", "Sports", "Party", "Business", "Children", "Hygge"];
const userList = [
new User ("bob123", "12345", "Bob", "bobiski", "bob@bob.com", "2000-10-10", "male", interestList[0]),
new User ("jack20", "340302", "Jack", "jackiski", "jack@jack.com", "2000-06-10", "male", interestList[0]),
new User ("jane20", "340303", "Jane", "janinski", "jane@jane.com", "2000-06-10", "famale", interestList[0]),
new User ("thif20", "204903", "Thifanny", "Thifiski", "thif@jack.com", "2000-09-12", "famale", interestList[1])
];
// const [ bob, jack, jane, thif ] = userList;
function renderUsersOfSameInterest(interest, userList) {
const [ first, ...rest ] = userList;
let template;
if (!rest.length) {
template = `There is no other user who matches ${ first }'s interest (${ interest }).`;
} else {
template = `${ first }'s interest (${ interest }) maches with the one of ${ rest.join(' and ') }!`;
}
console.log(template);
}
function renderUsersOfSameInterestsByFirstname(index) {
Object.entries(index).forEach(([interest, userList]) =>
renderUsersOfSameInterest(interest, userList.map(user =>
user.firstname))
);
// console.log('index', index);
}
renderUsersOfSameInterestsByFirstname(
userList.reduce(groupUserByInterest, {})
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

最新更新