jQuery 连接变量,如果两者都有一个值?



我真的希望这不是重复的,尽管肯定是可能的,我只是没有搜索正确的术语。

我正在开发基于飞行汽车版本的密码强度检查器。

我正在尝试找出一种组合两个变量的方法(仅当两个变量都存在时(。

这是我得到的:

var thisVal = $('.password_input').val(),
thisStrength = zxcvbn(thisVal),
thisMeter = $('.password_strength_meter'),
thisLabel = $('.password-text'),
thisSuggestion = $('.password-text-suggest');
thisSuggestion.text(thisStrength.feedback.warning + ". " thisStrength.feedback.suggestions);

如果

最好的方法真的是做多个if语句吗?或者有没有办法将其内联到 .text(( 部分?

我正在考虑进一步扩展这一点,包括破解所需的时间:

thisSuggestion.text(thisStrength.feedback.warning + ". This password could be cracked in: " + thisStrength.crack_times_display['online_no_throttling_10_per_second'] + ". " + thisStrength.feedback.suggestions);

所以希望可以避免多个 if 语句。

一种快速而干净的方法是将所有可能包含字符串的变量添加到数组中。然后,您可以过滤掉空白值或假值,并使用您选择的分隔符连接其余值。

const a = 'This will be there',
b = '',
c = null,
d = 'So will this';
let result = [a, b, c, d].filter(x => x).join('. ') + '.';
console.log(result);

相关内容

最新更新