[英]How do I use lodash to check every item in a collection except those that dont meet my condition?
let allChecked = _.every(this.collection, this.checked);
I have this existing code that returns true if every item in the collection has true for the checked
property.我有这个现有的代码,如果集合中的每个项目对于
checked
的属性都为 true,则返回 true。 I want to modify it so that instead of iterating on every item in the collection, only items that do not have true on another property are iterated on.我想修改它,而不是迭代集合中的每个项目,只迭代另一个属性上没有 true 的项目。 Ie, there is another property called
disabled
for the items in the collection.即,对于集合中的项目,还有另一个名为
disabled
的属性。 If this property is set to true, I would like to ignore those items completely from this _.every()
check.如果此属性设置为 true,我想从
_.every()
检查中完全忽略这些项目。
You could just call _.reject
on the this.collection
to remove any items that have true on the specified property in the collection.您可以在
_.reject
this.collection
删除集合中指定属性为 true 的任何项目。
An example would be like _.every(_.reject(this.collection, 'disabled'), this.checked)
一个例子就像
_.every(_.reject(this.collection, 'disabled'), this.checked)
Just add disabled
to the check with short circuit.只需在短路检查中添加
disabled
即可。 If disabled
is true
you can skip the check:如果
disabled
为true
,您可以跳过检查:
let allChecked = _.every(this.collection, obj => obj.disabled || this.checked(obj));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.