- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[]
和 {}
在 javascript 中是真实的。
但我想将它们视为 false,并且我很想知道如何以尽可能少的字符数来做到这一点:没有外部库或单独的函数,并且足够小以舒适地放在条件的括号之间。
换句话说:
What is the most concise way possible to check an unknown variable (that could be of any type) and return
false
if either of these apply: a) it's already falsy; or b) it's{}
or[]
?
最佳答案
数组最简洁的方式是!arr.length
。当然,这是假设您没有向数组添加任何非索引属性(这是一件完全有效的事情)。而作为 Qantas 94 Heavy在评论中指出,空数组可以将其 length
设置为非零值,而不会获得任何实际条目。
对象更棘手。什么是“空”对象?一个完全没有属性的?一个没有可枚举属性的?其原型(prototype)的属性(如果有)如何?
如果没有可枚举属性为你做,在 ES5 中你可以使用 !Object.keys(obj).length
。如果你不能指望 ES5,你必须有一个循环:
var empty = true;
var key;
for (key in obj) {
// Add an `obj.hasOwnProperty(key)` check here if you want to filter out prototype properties
empty = false;
break;
}
...这显然相当笨拙。
你说过你不想要单独的函数,但当然这是最好的方法:
var isFalsey = (function() {
var toString = Object.prototype.toString;
function isFalsey(x) {
var key;
if (!x) {
return true;
}
if (typeof x === "object") {
if (toString.call(x) === "[object Array]") {
return !x.length; // Assumes no non-element properties in the array
}
for (key in x) {
// Add an `x.hasOwnProperty(key)` check here if you want to filter out prototype properties
return false;
}
return true;
}
return false;
}
return isFalsey;
})();
关于javascript - JavaScript 中的 "Falsy or empty": How to treat {} and [] as false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19476236/
我有一个关于 javascript truthy/falsy 的问题 据我所知,包括负数在内的任何非零数都是真实的。但如果是这样,那为什么 -1 == true //returns false 还有
我正在尝试实现 Regula-Falsi 算法来求解 2(x^3)-x-2 的方程但问题是变量 c 值保持不变并且没有改变,即使我的代码应该改变它。 #include #include float f
我已经实现了 regula falsi 方法。我正在尝试对其进行修改,使其成为正割法。我读过的一份 pdf 提到它基本上是一样的,只是一个变化。 future 对我的“m”值的猜测应该有一个稍微不同的
我试图了解如何使用默认参数解构 falsy 和 null 值。以下是我运行过的一些示例: // #1 const person = { email: 'a@example.com' } const {
在 JavaScript 中有真值和假值的概念。 例如 0 :总是假的 1 : 始终为真 '0':始终为真 '1':始终为真 .NET 框架上的 C# 语言中是否有等效的真值和假值列表? 我想知道这个
据我所知,表达式不会在 Javascript 中的虚假值后执行。例如在下面的语句中: const result = undefined && 5; console.log(result); resul
我之前一直在阅读有关 JavaScript 提升的内容。 JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting
我需要一个值为零的属性(0) 防暴模板: this.time = condition ? '10' : '0' 期望的结果: 但是,如果属性值为假,Riot 会自动忽略整个属性: 我目前的解决
我希望编制一份 ColdFusion 认为虚假和真实的值的完整列表。我所知道的有: //falsy values false "false" 0 "no" //truthy values true "
Ruby 对“真”和“假”有一个普遍的看法。 Ruby 确实有两个用于 boolean 对象的特定类 TrueClass 和 FalseClass ,其单例实例分别由特殊变量 true 和 false
我想将任何虚假值变为未定义,这样就不会将特定属性添加到生成的 JSON 对象中。 const response = { myValue: 'someString', errorMessage:
[] 和 {} 在 javascript 中是真实的。 但我想将它们视为 false,并且我很想知道如何以尽可能少的字符数来做到这一点:没有外部库或单独的函数,并且足够小以舒适地放在条件的括号之间。
我在 Mocha 中进行的测试因此错误而失败。 我已将 catch 语句放在每个 Promise 链的末尾,这些语句会抛出它们传递的任何错误,但从未被调用。 但是测试仍然失败,并出现以下错误: Err
我也在这方面画了一个空白。如果有人能帮助我理解为什么我的代码没有打印出预期的输出,我将不胜感激: def bool_to_str(bval): if bval is True:
实现后Null Object Pattern在 Rails 应用程序中(也在 RubyTapas 第 112 集中描述)我重构了一些代码,但有一个语法结构似乎不再起作用。 我曾经写过像 current
undefined 和 null 在 javascript 中是假的,但是, var n = null; if(n===false){ console.log('null'); } else{ con
在 JavaScript 中,您可以使用很好的 .filter 方法从数组中删除 null 或 falsy 值。到目前为止,我还没有找到从 JavaScript 对象中删除相同内容的方法。 为什么会这
我想创建一个组件并公开某些可以被父元素覆盖的属性。 这是一个例子。假设我正在创建一个具有自己颜色的按钮,但如果定义了 --button-bg-color 则允许更改其背景颜色: .my-button
我收到此错误:使用 gcm 推送通知时接收者 key “registrationTokens”的假值。 您可以在下面找到我的代码: Device.find({ user: { $in: users }
我正在用带有 Node js 的 selenium mocha 进行单元测试,我是新手,我想用拒绝 promise 进行测试,但它给了我这个错误:Promise rejected with no or
我是一名优秀的程序员,十分优秀!