gpt4 book ai didi

Javascript 某些方法嵌套在 For 中

转载 作者:行者123 更新时间:2023-11-30 11:26:23 24 4
gpt4 key购买 nike

下面的代码我试图查看其中一个 CrmRoles 是否在 rolesToVerify 列表中,但是当我遍历它时,我一直为 roleToVerify 获取“undefined”。

const rolesToVerify = ["System Administrator", "Payroll"]

function userHasOneOrMoreRoles(currentUserRoles: CrmRole[], rolesToVerify: String[]) {
for (let currentUserRole of currentUserRoles) {
return rolesToVerify.some(roleToVerify =>
currentUserRole.Name === roleToVerify);
}
return false;
}

最佳答案

你在一个循环中返回,独立于任何条件或任何东西。没办法,您的循环会通过第一次迭代。

//And you can shorten
rolesToVerify.some(roleToVerify => currentUserRole.Name === roleToVerify);
//to
rolesToVerify.includes(currentUserRole.Name);

所以要么

function userHasOneOrMoreRoles(currentUserRoles: CrmRole[], rolesToVerify: String[]) {
for (let currentUserRole of currentUserRoles) {
if(rolesToVerify.includes(currentUserRole.Name))
return true;
}
return false;
}

function userHasOneOrMoreRoles(currentUserRoles: CrmRole[], rolesToVerify: String[]) {
return currentUserRoles.some(currentUserRole => rolesToVerify.includes(currentUserRole.Name));
}

关于Javascript 某些方法嵌套在 For 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47874277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com