英文:
How do i compare a [null] list?
问题
我有一个列表,返回[null],但我想用[]替换它
我正在使用useEffect(),但我真的找不到一个与null比较时返回true的表达式。
我尝试过:
listToCompare === null // false
listToCompare[0] === null // false
listToCompare === undefined // false
listToCompare === [null] // error even before compiling
这很烦人,因为[null]返回.length为1,而我需要它为0。
有人能帮我找到一个与[null]比较时返回true的表达式吗?
真的很困惑一个[null]列表到底是什么:(
英文:
I have a list which is returning [null] but I want to replace that with []
I'm using useEffect() but I'm literally not able to find an expression that returns true when compared with null.
I've tried:
listToCompare === null // false
listToCompare[0] === null // false
listToCompare === undefined // false
listToCompare === [null] // error even before compiling
This is irritating because [null] returns .length as 1 when I need it to be 0.
Can someone help me find an expression to return true when compared to [null]
Really confused on what a [null] list even is ![]()
答案1
得分: 0
以下是您要翻译的内容:
const listToCompare = [null];
const isNull = !listToCompare.filter(Boolean).length
console.log(isNull) // 返回 true
英文:
You could try
const listToCompare = [null];
const isNull = !listToCompare.filter(Boolean).length
console.log(isNull) // returns true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论