简体   繁体   English

我如何使用 lodash 检查集合中的每个项目,除了那些不符合我的条件的项目?

[英]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:如果disabledtrue ,您可以跳过检查:

let allChecked = _.every(this.collection, obj => obj.disabled || this.checked(obj));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 lodash 中的 includes 方法检查对象是否在集合中? - How do I use the includes method in lodash to check if an object is in the collection? 如果除了2之外的其他虚假,如何检查数组索引0上的每个项目? - How to check for every item on the index 0 of an array if are falsy except for 2? 如何检查 discord 的所有者和权限,以及他们是否具有这些特定角色? - How do i check for discord owner and permission and if they have those specific roles or not at one if condition? "如何显示我的 localStorage 项目中的每个元素?" - How do I display every element from my localStorage item? 如何检查数组中的最后一个字符是否满足条件 - How to check if the last characters in an array meet a condition 如何检查if语句的条件以查看其是否正常运行? - How do I check my condition on an if statement to see if it is working correctly? 如何使用lodash在集合中查找特定结果? - how to use lodash to find specific result in collection? 如何使用lodash.keyBy更改集合中每个项目的标识符? - How can I change the identifier of each item in collection with lodash.keyBy? 如何在EmberJS应用程序中使用Jitsi Meet - How do I use Jitsi Meet in an EmberJS app 我如何在 Lodash 中做到这一点? - How do I do this in Lodash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM