我正在 Vannila JS 中制作随机密码生成器,我在以下代码中面临未知问题:
function generatePassword(lower, upper, number, symbol, length) {
let generatedPassword = "";
const typesCount = lower + upper + number + symbol;
const typesArr = [{ lower }, { upper }, { number }, { symbol }].filter(
(item) => Object.values(item)[0]
);
// Doesn't have a selected type
if (typesCount === 0) {
return "";
}
// create a loop
for (let i = 0; i < length; i += typesCount) {
typesArr.forEach((type) => {
const funcName = Object.keys(type)[0];
generatedPassword += randomFunc[funcName]();
});
}
const finalPassword = generatedPassword.slice(0, length);
return finalPassword;
}
在此代码块中,如果我将语句括在大括号 {} 中,则Object.values(item)[0]将不起作用:
const typesArr = [{ lower }, { upper }, { number }, { symbol }].filter(
(item) => Object.values(item)[0]
);
我不明白为什么会发生这种情况,我尝试搜索文档但无济于事,谢谢!
慕田峪9158850
相关分类