# singleBy

Source: https://taxilang.org/docs/language/stdlib/singleBy

**Full name:** `taxi.stdlib.singleBy`

## Signature

```taxi
declare extension function <T,A> singleBy(collection:T[], groupingFunction: (T) -> A, searchValue: A):T
```

## Description

Returns the item from a collection where the value returned by the grouping function matches the search value.

Similar to `single()`, but results are first grouped by the selector function, and these results are cached
to improve future performance when querying the same collection multiple times.

```taxi
// Find a product by its SKU code using a cached search
find { inventory: Inventory } as {
   appleMacbook: inventory.products.singleBy((product) -> product.sku, "MB-PRO-2023")
}
```

## Examples

Looks up products by SKU using a cached index. Faster than `single()` when querying the same collection multiple times.

Schema:

```taxi
model Product {
   name: Name inherits String
   sku: SKU inherits String
}
model Inventory {
   products: Product[]
}
```

Query:

```taxi
given {
   inventory: Inventory = {
      products: [
         { name: 'Laptop', sku: 'LT-001' },
         { name: 'Smartphone', sku: 'SP-002' },
         { name: 'Tablet', sku: 'TB-003' }
      ]
   }
}
find {
   laptop: inventory.products.singleBy((Product) -> SKU, 'LT-001' as SKU)
   tablet: inventory.products.singleBy((Product) -> SKU, 'TB-003' as SKU)
}
```

Result:

```json
{
  "laptop": {
    "name": "Laptop",
    "sku": "LT-001"
  },
  "tablet": {
    "name": "Tablet",
    "sku": "TB-003"
  }
}
```
