# Cratedb loop through array

**URL:** <https://community.cratedb.com/t/cratedb-loop-through-array/1062>\
**Category:** SQL\
**Created:** [March 24, 2022, 9:35pm UTC](https://community.cratedb.com/t/cratedb-loop-through-array/1062 "2022-03-24T21:35:17Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![hammerhead](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/hammerhead/32/270_2.png) [@hammerhead](https://community.cratedb.com/u/hammerhead)\
**Post date:** [March 25, 2022, 6:50am UTC](https://community.cratedb.com/t/cratedb-loop-through-array/1062/2 "2022-03-25T06:50:51Z")

</div>

Hi @arafaraf and welcome to our community!

This can be solved with a [User-Defined Function](https://community.cratedb.com/t/a-collection-of-useful-user-defined-functions-udfs/773).

If you want to find the elements in the array, you could use a function like this:

```sql
CREATE OR REPLACE FUNCTION array_filter(ARRAY(INTEGER), INTEGER, INTEGER) RETURNS ARRAY(INTEGER)
LANGUAGE JAVASCRIPT
AS 'function array_filter(array_integer, min_value, max_value) {
    return Array.prototype.filter.call(array_integer, element => element >= min_value && element <= max_value);
}';

SELECT array_filter([5, 7, 20], 2, 8) 
-- returns [5, 7]

```

If you only want to identify if there is a value within the given boundaries, you can also do this:

```sql
CREATE OR REPLACE FUNCTION array_find(ARRAY(INTEGER), INTEGER, INTEGER) RETURNS BOOLEAN
LANGUAGE JAVASCRIPT
AS 'function array_find(array_integer, min_value, max_value) {
    return Array.prototype.find.call(array_integer, element => element >= min_value && element <= max_value) !== undefined;
}';

SELECT array_find([5, 7, 20], 5, 300);
-- returns true

SELECT array_find([5, 7, 20], 25, 300);
-- returns false

```

–  
Best

Niklas

---

_[View the full topic](https://community.cratedb.com/t/cratedb-loop-through-array/1062)._
