# Tip: Use JavaScript to filter on a value in a record array

**URL:** <https://connective.celigo.com/t/tip-use-javascript-to-filter-on-a-value-in-a-record-array/1068>\
**Category:** Integrator.io\
**Tags:** filter, javascript\
**Created:** [August 5, 2021, 7:03pm UTC](https://connective.celigo.com/t/tip-use-javascript-to-filter-on-a-value-in-a-record-array/1068 "2021-08-05T19:03:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomsantiago](https://sea1.discourse-cdn.com/flex001/user_avatar/connective.celigo.com/tomsantiago/32/7158_2.png) [@tomsantiago](https://connective.celigo.com/u/tomsantiago)\
**Post date:** [August 5, 2021, 7:03pm UTC](https://connective.celigo.com/t/tip-use-javascript-to-filter-on-a-value-in-a-record-array/1068/1 "2021-08-05T19:03:13Z")

</div>

Use the following JavaScript code to filter records on the presence or absence of a given value in a record array. When applying this filtration strategy, you must enable the JavaScript option in order to add the function. Click the JavaScript toggle filter at the top of the filter editor.

![5301043caa20740200000001-0ba1ba81a1074b94b2c1575899a5e2a0.png](https://us1.discourse-cdn.com/flex001/uploads/celigo/original/2X/0/01e83ba0dcc3e13a7df7637f8d364a8b3f6bac2d.png)

Here is an example record:

```
{"record": { "orderId": 123, "total": 99.99, "state": "ca", "items": [{ "description": "Hat", "qty": 3 }, { "description": "Shoes", "qty": 1 }]}}
```

Here is the JavaScript function:

```
function filter(options) { for (var i = 0; i < options.record.items.length; i++) { if (options.record.items[i].description === "Hat") { return false; } } return true; }
```

If the function is set to "false" the record will be ignored. If the function returns "true" the record will be processed.

---

<div class="post-metadata">

**Author:** ![psaadmin](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@psaadmin](https://connective.celigo.com/u/psaadmin)\
**Post date:** [August 13, 2021, 4:05pm UTC](https://connective.celigo.com/t/tip-use-javascript-to-filter-on-a-value-in-a-record-array/1068/2 "2021-08-13T16:05:10Z")

</div>

Thank you, Tom! Question: is the filter either J-Script or Rule but not both? I'm guessing that's they are mutually exclusive, but just wanted to verify.

Jim

---

<div class="post-metadata">

**Author:** ![jolyonblazey](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@jolyonblazey](https://connective.celigo.com/u/jolyonblazey)\
**Post date:** [December 18, 2023, 10:24pm UTC](https://connective.celigo.com/t/tip-use-javascript-to-filter-on-a-value-in-a-record-array/1068/3 "2023-12-18T22:24:08Z")

</div>

I am pretty sure you can only use one or the other not both.

Thanks for this helpful code, this is an alternative filter searching for different criteria using reduce()

```
function filterIncludeReturnShipping (options) { if (!options.record || !options.record.line_items) { return false } const returnWillBeShippedToWarehouse = 'default' const initialValue = false return options.record.line_items.reduce((accumulator, currentValue) => { return accumulator || ( options.record.state === "open" && currentValue.outcome === returnWillBeShippedToWarehouse )}, initialValue) }
```
