Files
30-seconds-of-code/snippets/any.md
2022-12-04 22:20:49 +02:00

22 lines
581 B
Markdown

---
title: Test if any array element is truthy
tags: array
cover: blog_images/basket-paper.jpg
firstSeen: 2018-02-14T11:46:15+02:00
lastUpdated: 2020-10-18T20:24:28+03:00
---
Checks if the provided predicate function returns `true` for at least one element in a collection.
- Use `Array.prototype.some()` to test if any elements in the collection return `true` based on `fn`.
- Omit the second argument, `fn`, to use `Boolean` as a default.
```js
const any = (arr, fn = Boolean) => arr.some(fn);
```
```js
any([0, 1, 2, 0], x => x >= 2); // true
any([0, 0, 1, 0]); // true
```