javascript 筛选数组始终为空

wkftcu5l  于 2023-03-16  发布在  Java
关注(0)|答案(3)|浏览(151)

我试着做一些过滤逻辑,并让项目的成员im上(根据它的名字在url参数),但问题是,它总是空的,你可以看到下面(teamMembers总是空的,即使我改变过滤条件,以一些明显的).我也尝试了其他方法,而不是过滤器上的allUsers数组,他们只是罚款不知何故.

function Team({ setShowAddMembers }: props) {
    const params = useParams();
    const { projectName } = params;

    const [projectTeam, setProjectTeam] = useState<user[]>([]);

    useEffect(() => {
        try {
            const getProjectTeam = async () => {
                const allProjects: project[] = await getAllProjects();
                console.log(allProjects); // works. Logs projects array
                const allUsers: user[] = await getAllUsers();
                console.log(allUsers); // works. Logs users array

                // to get only the project of the page you're on
                const currentProject: project[] = allProjects.filter(
                    (project) => project.name === projectName
                );
                console.log(currentProject); // works as intended. Logs a 1 item array
                console.log(currentProject[0]); // works as intended 
                const { members } = currentProject[0];
                console.log(members); //works. Logs array of the members of the current project
                const teamMembers: user[] = allUsers.filter((user) =>
                    members.includes(user.username)
                );
                console.log(teamMembers); // DOES NOT WORK. LOGS EMPTY ARRAY
                setProjectTeam(teamMembers);
            };
            getProjectTeam();
        } catch (error) {
            console.log(error);
        }
    }, []);

控制台日志:(按代码中的顺序排列)

  • 所有项目:[{...},{...},{...},{...},{...},{...},{...}]一个包含7个此类对象的数组:
{
        "tickets": [],
        "_id": "640f80cbd601d54a956de834",
        "desc": "Marketplace website",
        "name": "Market",
        "members": [
            "John Doe",
            "Dave Gray"
        ]
    }
  • allUsers:[{...},{...},{...},{...}]返回以下类型对象的数组:
{
    "_id": "640c6e87d8efde2fabbb1ba2",
    "username": "AdminDemo",
    "email": "admindemo@gmail.com",
    "password": "$2b$10$7O7JZVgYW9k0dOXiAFtWv.jN7VVmoooPagOvnEnfEfSB7cTH0md/S",
    "role": "admin",
    "protected": true,
    "__v": 0
}
  • 当前项目:
[
    {
        "tickets": [],
        "_id": "640f80cbd601d54a956de834",
        "desc": "Marketplace website",
        "name": "Market",
        "members": [
            "John Doe",
            "Dave Gray"
        ]
    }
]
  • 当前项目[0]:
{
        "tickets": [],
        "_id": "640f80cbd601d54a956de834",
        "desc": "Marketplace website",
        "name": "Market",
        "members": [
            "John Doe",
            "Dave Gray"
        ]
    }
  • 委员:
["John Doe", "Dave Gray"]
  • 团队成员:
[]
m3eecexj

m3eecexj1#

看起来你的问题是,你试图比较members名称,如John Doe与用户的用户名,如AdminDemo的方式给出的例子,我敢打赌,这些永远不会匹配。
我不知道你如何建立用户名相比,成员的名字,如果它只删除名字和姓氏之间的空间,你可以做一个黑客的解决方案,只是删除成员的名字的空间。

const teamMembers: user[] = allUsers.filter((user) =>
    members.some(m => m.replace(" ", "") === user.userName)
);

但是,建议将userId存储为成员,以缓解将来用户具有相同名称等的进一步问题。

new9mtju

new9mtju2#

我认为您可以通过查看您的成员(我假设这是一个成员列表)并在以下行中写入一些内容来解决问题

members.some(m => m.username === user.username);

一些函数遍历数组,如果条件返回真则返回,否则返回假。

a64a0gku

a64a0gku3#

您可以添加一些console.log语句来检查成员和用户名数组的值,以确保它们不为空并包含有效数据。以下是更新的代码片段,可以帮助您调试问题:

const { members } = currentProject[0];
console.log(members); // works. Logs array of the members of the current project

const teamMembers: user[] = allUsers.filter((user) => {
    console.log(user.username); // log the current user's username
    console.log(members.includes(user.username)); // log whether the members array includes the current user's username
    return members.includes(user.username);
});
console.log(teamMembers); // logs the teamMembers array

相关问题