计算布尔值的数量(JavaScript)
让我的对象具有以下数据:
{
mongoose: {
name: "Starky",
age: 3,
playful: false
},
dog: {
name: "Maks",
age: 7,
playful: false
},
parrot: {
name: "Petty",
age: 4,
playful: true
}
}
我如何计算布尔人的数量?
我知道我可以使用。reduce
,但是我不明白如何正确使用它,为了尝试使用它,我只是得到了undefined
。
Let my object have the following data:
{
mongoose: {
name: "Starky",
age: 3,
playful: false
},
dog: {
name: "Maks",
age: 7,
playful: false
},
parrot: {
name: "Petty",
age: 4,
playful: true
}
}
How can I count the number of booleans?
I know that I can use .reduce
, but I didn’t understand exactly how to use it correctly, in an attempt to use it, I just got undefined
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里,
Here,
您可以做类似的事情
来检查它是否是布尔值,我已经使用了
!!
和=====
you can do something like this
to check if it is a boolean I have used
!!
and===
您可以使用两次以计算所有布尔值。
You can use Array.prototype.reduce twice to count all the boolean values.