@empathyco/x-components
Version:
Empathy X Components
97 lines (80 loc) • 2.29 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@empathyco/x-components](./x-components.md) > [deepFilter](./x-components.deepfilter.md)
## deepFilter() function
Filters an array with all elements that pass the test implemented by the given filter function. If an item has another list of items in the `childrenKey` property it recursively filters that new list, adding it to the returned one.
**Signature:**
```typescript
export declare function deepFilter<Item extends {
[key in Key]?: Item[];
}, Key extends keyof Item>(array: Item[], condition: (item: Item) => boolean, childrenKey: Key): Item[];
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| array | Item\[\] | Array to be filtered. |
| condition | (item: Item) => boolean | Predicate function to test each element of the array. It should return <code>true</code> to keep the element; or <code>false</code> otherwise. |
| childrenKey | Key | Property name within the array used to perform a recursive call. |
**Returns:**
Item\[\]
A new array with the elements that pass the condition, or an empty array if no one pass the test.
## Example
Input - Output example
```
const hierarchicalFilters: Filter[] = [
{
id: 'filter1'
selected: true,
children: [
{
id: 'filter1-1'
selected: true,
children: []
},
{
id: 'filter1-2'
selected: false,
children: []
}
]
},
{
id: 'filter2',
selected: false,
children: [
{
id: 'filter2-1',
selected: true // not should happen
}
]
}
]
const filteredArray: Filter[] = deepFilterArray(
hierarchicalFilters,
filter => filter.selected,
'children'
)
/*
filteredArray = [
{
id: 'filter1'
selected: true,
children: [
{
id: 'filter1-1'
selected: true,
children: []
},
{
id: 'filter1-2'
selected: false,
children: []
}
]
},
{
id: 'filter1-1'
selected: true,
children: []
}
]
```