是否有一个通用的JavaScript函数来检查变量是否有值,并确保它不是未定义的或空的?我有这个代码,但我不确定它是否涵盖所有情况:
function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}
是否有一个通用的JavaScript函数来检查变量是否有值,并确保它不是未定义的或空的?我有这个代码,但我不确定它是否涵盖所有情况:
function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}
当前回答
这是我检查数据是否为空的解决方案。
const _isEmpty = (data) => {
return (
// this way we can also check for undefined values. null==undefined is true
data == null ||
data == "" ||
(Array.isArray(data) && data.length === 0) ||
// we want {} to be false. we cannot use !! because !!{} turns to be true
// !!{}=true and !!{name:"yilmaz"}=true. !! does not work ofr objects
(data.constructor === Object && Object.keys(data).length === 0)
);
};
其他回答
对于我的用例,大多数现有答案都失败了,如果将函数分配给变量或返回NaN,则大多数答案返回为空。帕斯卡的回答很好。
这是我的实现,请测试并让我知道,如果你发现了什么。你可以在这里看到我是如何测试这个函数的。
function isEmpty(value) {
return (
// Null or undefined.
(value == null) ||
// Check if a Set() or Map() is empty
(value.size === 0) ||
// NaN - The only JavaScript value that is unequal to itself.
(value !== value) ||
// Length is zero && it's not a function.
(value.length === 0 && typeof value !== "function") ||
// Is an Object && has no keys.
(value.constructor === Object && Object.keys(value).length === 0)
)
}
退货:
true:undefined,null,“”,[],{},NaN,new Set()//false:true,false,1,0,-1,“foo”,[1,2,3],{foo:1},函数(){}
仅使用“空值合并”检查未定义和空值
if ((myVariable ?? undefined) !== undefined) {
// handle myVariable has a value, including 0 or ""
}
else {
// handle undefined or null only
}
来自铬控制台
{const x=undefined; (x ?? undefined) !== undefined}
false
{const x=null; (x ?? undefined) !== undefined}
false
{const x=0; (x ?? undefined) !== undefined}
true
{const x=""; (x ?? undefined) !== undefined}
true
{const x={}; (x ?? undefined) !== undefined}
true
{const x=[]; (x ?? undefined) !== undefined}
true
{const x="a"; (x ?? undefined) !== undefined}
true
下面是我的工作。请稍微改变一下,以加快速度
function isEmpty(obj) {
if (!obj) return true;
if (typeof obj == 'number') return false;
else if (typeof obj == 'string') return obj.length == 0;
else if (Array.isArray(obj)) return obj.length == 0;
else if (typeof obj == 'object') return obj == null || Object.keys(obj).length == 0;
else if (typeof obj == 'boolean') return false;
}
return val || 'Handle empty variable'
是在许多地方处理它的一种非常好且干净的方法,也可以用于分配变量
const res = val || 'default value'
如果您喜欢纯javascript,请尝试以下操作:
/**
* Checks if `value` is empty. Arrays, strings, or `arguments` objects with a
* length of `0` and objects with no own enumerable properties are considered
* "empty".
*
* @static
* @memberOf _
* @category Objects
* @param {Array|Object|string} value The value to inspect.
* @returns {boolean} Returns `true` if the `value` is empty, else `false`.
* @example
*
* _.isEmpty([1, 2, 3]);
* // => false
*
* _.isEmpty([]);
* // => true
*
* _.isEmpty({});
* // => true
*
* _.isEmpty('');
* // => true
*/
function isEmpty(value) {
if (!value) {
return true;
}
if (isArray(value) || isString(value)) {
return !value.length;
}
for (var key in value) {
if (hasOwnProperty.call(value, key)) {
return false;
}
}
return true;
}
否则,如果您已经使用下划线或lodash,请尝试:
_.isEmpty(value)