如何比较一个 [null] 列表?

huangapple go评论74阅读模式
英文:

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]返回.length1,而我需要它为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 如何比较一个 [null] 列表?

答案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

huangapple
  • 本文由 发表于 2023年4月4日 10:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定