---
title: GROUP_BY
url: https://www.tines.com/docs/formulas/functions/group-by/
kind: formula-function
---

*[tines.com](https://www.tines.com/llms.txt) › [Docs](https://www.tines.com/llms.txt) › [Formulas](https://www.tines.com/llm/docs/formulas.md) › [Functions](https://www.tines.com/llm/docs/formulas/functions.md)*

# GROUP_BY

*[View on tines.com](https://www.tines.com/docs/formulas/functions/group-by/)*

Groups an array of objects by a key or the result of a `LAMBDA` function. `as_array` is a boolean keyword argument (`TRUE` / `FALSE`, default `FALSE`). When `as_array` is `TRUE`, `GROUP_BY` returns an array of objects with `key` and `values` properties. When `as_array` is `FALSE`, it returns an object keyed by group.

**Categories:** Arrays

## Syntax

```
GROUP_BY(items, key_or_lambda, [as_array: FALSE])
```

## Examples

### Example 1

Input:

```json
{
  "employees": [
    {
      "name": "Alice",
      "office": "Dublin"
    },
    {
      "name": "Bob",
      "office": "Dublin"
    },
    {
      "name": "Charlie",
      "office": "New York"
    },
    {
      "name": "Dave",
      "office": "New York"
    }
  ]
}
```

Formula:

```
GROUP_BY(employees, "office")
```

Output:

```json
{
  "Dublin": [
    {
      "name": "Alice",
      "office": "Dublin"
    },
    {
      "name": "Bob",
      "office": "Dublin"
    }
  ],
  "New York": [
    {
      "name": "Charlie",
      "office": "New York"
    },
    {
      "name": "Dave",
      "office": "New York"
    }
  ]
}
```

### Example 2

Input:

```json
{
  "alerts": [
    {
      "name": "Failed Login",
      "severity": 8,
      "source": "firewall"
    },
    {
      "name": "Config Change",
      "severity": 5,
      "source": "audit"
    },
    {
      "name": "Disk Space Low",
      "severity": 3,
      "source": "monitoring"
    },
    {
      "name": "Malware Detected",
      "severity": 9,
      "source": "endpoint"
    }
  ]
}
```

Formula:

```
GROUP_BY(alerts, LAMBDA(alert, IF(alert['severity'] > 7, 'high', IF(alert['severity'] > 4, 'medium', 'low'))))
```

Output:

```json
{
  "high": [
    {
      "name": "Failed Login",
      "severity": 8,
      "source": "firewall"
    },
    {
      "name": "Malware Detected",
      "severity": 9,
      "source": "endpoint"
    }
  ],
  "medium": [
    {
      "name": "Config Change",
      "severity": 5,
      "source": "audit"
    }
  ],
  "low": [
    {
      "name": "Disk Space Low",
      "severity": 3,
      "source": "monitoring"
    }
  ]
}
```

### Example 3

Input:

```json
{
  "employees": [
    {
      "name": "Alice",
      "office": "Dublin"
    },
    {
      "name": "Bob",
      "office": "Dublin"
    },
    {
      "name": "Charlie",
      "office": "New York"
    },
    {
      "name": "Dave",
      "office": "New York"
    }
  ]
}
```

Formula:

```
GROUP_BY(employees, 'office', as_array: TRUE)
```

Output:

```json
[
  {
    "key": "Dublin",
    "values": [
      {
        "name": "Alice",
        "office": "Dublin"
      },
      {
        "name": "Bob",
        "office": "Dublin"
      }
    ]
  },
  {
    "key": "New York",
    "values": [
      {
        "name": "Charlie",
        "office": "New York"
      },
      {
        "name": "Dave",
        "office": "New York"
      }
    ]
  }
]
```
