简体   繁体   中英

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. 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. Ie, there is another property called disabled for the items in the collection. If this property is set to true, I would like to ignore those items completely from this _.every() check.

You could just call _.reject on the this.collection to remove any items that have true on the specified property in the collection.

An example would be like _.every(_.reject(this.collection, 'disabled'), this.checked)

Just add disabled to the check with short circuit. If disabled is true you can skip the check:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM