我如何从 "complex" 条件与 && 或 || 的组合中编写一个干净的条件
How I write a clean condition from a "complex" combinations of conditions with && or ||
比如a有两个语句条件:
首先,类似于
const condition1 = value === '' && value1 === '' && value2 === '' && valu3e === '' && value4 === '' && value5 === ''
第二个条件更简单像
const condition2 = value === 'error'
我的 if 语句有两个这样的:
if (condition1 || condition2) {
....
}
有什么方法可以使它比实际实施更好?
第一个条件:
const value = '',
value1 = '',
value2 = '',
value3 = '',
value4 = '',
value5 = '';
const condition1 = [
value,
value1,
value2,
value3,
value4,
value5,
].every(x => x === '');
console.log(`Condition1 : ${condition1}`);
如果你能将这些值重构为一个数组,那就更好了。
const values = [
'',
'',
'',
'',
'',
'',
];
const condition1 = values.every(x => x === '');
console.log(`Condition1 : ${condition1}`);
一共
const value = '',
value1 = '',
value2 = '',
value3 = '',
value4 = '',
value5 = '';
if ([
value,
value1,
value2,
value3,
value4,
value5,
].every(x => x === '') || value === 'error') {
console.log(`Condition : true`);
} else {
console.log(`Condition : false`);
}
为什么不简单:
if ( (value === '' && value1 === '' && value2 === '' && valu3e === '')
|| value === 'error'
) {
/// ...
}
?
比如a有两个语句条件:
首先,类似于
const condition1 = value === '' && value1 === '' && value2 === '' && valu3e === '' && value4 === '' && value5 === ''
第二个条件更简单像
const condition2 = value === 'error'
我的 if 语句有两个这样的:
if (condition1 || condition2) {
....
}
有什么方法可以使它比实际实施更好?
第一个条件:
const value = '',
value1 = '',
value2 = '',
value3 = '',
value4 = '',
value5 = '';
const condition1 = [
value,
value1,
value2,
value3,
value4,
value5,
].every(x => x === '');
console.log(`Condition1 : ${condition1}`);
如果你能将这些值重构为一个数组,那就更好了。
const values = [
'',
'',
'',
'',
'',
'',
];
const condition1 = values.every(x => x === '');
console.log(`Condition1 : ${condition1}`);
一共
const value = '',
value1 = '',
value2 = '',
value3 = '',
value4 = '',
value5 = '';
if ([
value,
value1,
value2,
value3,
value4,
value5,
].every(x => x === '') || value === 'error') {
console.log(`Condition : true`);
} else {
console.log(`Condition : false`);
}
为什么不简单:
if ( (value === '' && value1 === '' && value2 === '' && valu3e === '')
|| value === 'error'
) {
/// ...
}
?