How to use the any function from underscore

Find comprehensive JavaScript underscore.any code examples handpicked from public code repositorys.

underscore.any is a function in the Underscore.js library that checks if at least one element in a collection satisfies a condition.

176
177
178
179
180
181
182
183
184
185
 * Checks whether the passes card is in a legal location for the passed type of play
 * @param card
 * @param {String} playingType
 */
isCardInPlayableLocation(card, playingType) {
    return _.any(
        this.playableLocations,
        (location) =>
            (!playingType || location.playingType === playingType) && location.contains(card)
    );
fork icon9
star icon10
watch icon1

215
216
217
218
219
220
221
222
223
    const query = {
        fields: ['admins'],
        filters: [['_id', '=', companyIds], ['space', '=', userSession.spaceId]]
    }
    const companys = await getObject("company").find(query);
    isAdmin = _.any(companys, (item) => {
        return item.admins && item.admins.indexOf(userSession.userId) > -1
    })
}
fork icon0
star icon0
watch icon1

+ 3 other calls in file

How does underscore.any work?

_.any (or _.some) is an Underscore.js function that iterates over a list of elements and returns true if at least one of the elements passes the callback test, and false otherwise. It stops iterating as soon as a passing element is found. The callback function is passed the current element and its index, and is evaluated in the context of the optional thisArg. If no thisArg is provided, _.any uses the this value of the current scope. The function returns false if called on an empty list.

1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
},
matchObject: function (obj) {
  if (_.isEmpty(obj) || _.size(obj) > 2) {
    return false;
  }
  return _.any(builtinConverters, function (converter) {
    return converter.matchJSONValue(obj);
  });
},
toJSONValue: function (obj) {
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
const arr = [2, 4, 6, 8, 9];
const result = _.any(arr, (num) => num % 2 !== 0);

console.log(result); // true

In this example, _.any (or _.some) checks whether any element of the arr array is odd or not. Since the number 9 is present in the array, which is odd, the result of _.any is true.