我刚刚编写了以下代码,以查找数组列表中的第一个匹配项。有什么有效的方法吗?
function hasName(names, req)
{
let state = 'pending';
for(let i in names)
{
if(req.indexOf(names[i]) !== -1)
{
state = 'init';
break;
}
}
return state;
}
hasName(['A', 'B', 'C'], ['B', 'D']);
答案 0 :(得分:2)
names.some(item => req.includes(item))
如果在2个数组中发现同一元素的任何出现,则返回true,否则返回false
答案 1 :(得分:0)
希望这对您有用
const hasName = (names, req) => names.some(name => req.includes(name)) ? 'init' : 'pending';
hasName(['A', 'B', 'C'], ['B', 'D']);