3 回答
TA贡献1818条经验 获得超3个赞
只需检查值并使用逻辑或链接条件。
const check = (a, b) => a === null || a === '' || b === null || b === '';
// the above is the same as this, like the text requests
// (a === null || a === '') !== (b === null || b === '') ||
// (a === null || a === '') && (b === null || b === '');
console.log([
[null, null], // true
[null, ''], // true
['', null], // true
['', ''], // true
[1, 1], // false
[null, 1], // true
[1, null] // true
].map(a => check(...a)));
TA贡献1802条经验 获得超6个赞
可以在分配 null 或 "" 之一时进行简化
let test = (a = "", b = "") => {
if(a === null) a = ""
if(b === null) b = ""
return !( a != '' && b != '')
}
console.log(test(null, null))
console.log(test('', ''))
console.log(test('a','b'))
console.log(test(null,'b'))
添加回答
举报