---
title: "findById"
description: "Find a single tree node by its id using the configured id field, returning the node or undefined when no match exists."
canonical_url: "https://core.mhaibaraai.cn/en/docs/transformers/tree/find-by-id"
---
# findById

> Find a single tree node by its id using the configured id field, returning the node or undefined when no match exists.

## Usage

The `findById` method quickly finds and returns a node by its `id`. This is an efficient lookup operation.

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

const tree = [{ id: 1, name: 'A', children: [{ id: 2, name: 'B' }] }]

const node = Tree.findById(tree, 2)

// node => { id: 2, name: 'B' }
```

## API

`findById<T extends TreeNode>(tree: T[], id: number | string, config?: TreeConfig): T | undefined`

### Parameters

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

**id** (`number | string`) *required*: The ID of the node to find.

**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 | undefined**: Returns the found node, or undefined if no match is found.

## 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.
