一个最好的分组和渲染项目如何进行渲染,例如在许多“用户”实例中可能具有相等的“兴趣”属性?

问题描述

大家好,我在尝试创建函数时遇到一些问题。如代码所示。我想做interestMatch函数。该功能要做的是查看所有“用户”并找到那些具有相同兴趣的用户-我的代码中的“ bob”和“ jack”。我认为应该带有if语句。 Kinda:“如果有任何用户有相同的兴趣”“返回此用户,并且此用户为匹配对象!” 有人能帮我吗!? 非常感谢!

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",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语句的东西

一种有效的方法是浏览用户列表,查找用户的每个兴趣并将其推入与兴趣相关的数组。因此,基本方法使用特定兴趣的用户列表创建基于<DataGrid x:Name="dgListings" HeadersVisibility="Column" ItemsSource="{Binding LoadedListings}" CanUserAddRows="False" AutoGenerateColumns="False"> 的地图或索引。

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

掌握了所需的数据结构(一个键值存储,其中interest是键,一个特定的用户列表是每个键的值),其中一个接近第二部分,即输出/呈现。

对于每个键值对(兴趣和用户列表),确实会调用特定于用户列表的呈现功能。在这一步骤中,也确实将interest列表映射到一个列表,例如每个用户的名字。

然后,最终的渲染功能需要处理边缘情况……例如……有一个用户,而其他任何人都没有共享的兴趣……或者……有一个共同兴趣并且拥有相同兴趣的两个用户...

user
class User {
  constructor(
    username,password,firstname,lastname,email,birthday,gender,interest
  ) {
    Object.assign(this,{
      username,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",new User ("jane20","340303","Jane","janinski","jane@jane.com","famale",new User ("thif20","204903","Thifanny","Thifiski","thif@jack.com","2000-09-12",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,{})
);