---
title: "findAll"
description: "Find every node in a tree that matches a predicate, returning a flat array of matches across all branches and depth levels."
canonical_url: "https://core.mhaibaraai.cn/en/docs/transformers/tree/find-all"
---
# findAll

> Find every node in a tree that matches a predicate, returning a flat array of matches across all branches and depth levels.

## Usage

The `findAll` method performs a depth-first search on the tree and returns an array of all nodes that satisfy the `predicate` function.

```ts [example.ts]
import { Tree } from '@movk/core'

const tree = [{ id: 1, type: 'folder', children: [{ id: 2, type: 'file' }, { id: 3, type: 'folder' }] }]

const folders = Tree.findAll(tree, ({ node }) => node.type === 'folder')

// folders => [{ id: 1, type: 'folder', ... }, { id: 3, type: 'folder' }]
```

## API

`findAll<T extends TreeNode>(tree: T[], predicate: (context: VisitorContext<T>) => boolean, config?: TreeConfig): T[]`

### Parameters

**tree** (`T[]`) *required*: The source tree array.

**predicate** (`(context: VisitorContext<T>) => boolean`) *required*: A predicate function called for each node in the tree. If the function returns true, that node and all its ancestor nodes will be retained in the result.
The function receives a context object with the following properties:The node currently being processed.The depth of the node (root node is 0).An array of nodes from the root to the current node (inclusive).The index of the current node among its siblings.

**config** (`TreeConfig`): Configuration object for customizing the id, pid, children key names in the tree structure.Optional. The key name for the node's unique identifier. Defaults to "id".Optional. The key name for the node's parent identifier. Defaults to "pid".Optional. The key name for the children array. Defaults to "children".

### Returns

**T[]**: Returns an array containing all matching nodes.

## Changelog

See commit history for [src/transformers/tree/query.ts](https://github.com/mhaibaraai/movk-core/commits/main/src/transformers/tree/query.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-core/blob/main/src/transformers/tree/query.ts)


## Sitemap

See the full [sitemap](https://core.mhaibaraai.cn/sitemap.md) for all pages.
