# flat-group

> A function for creating flat groups from items in array

Latest version **0.0.9** (published 2020-07-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install flat-group
pnpm add flat-group
yarn add flat-group
bun add flat-group
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2020-07-23 |
| First published | 2020-07-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | James Barford-Evans |
| Maintainers | jamesbarford |

## Links

- npm: https://www.npmjs.com/package/flat-group
- Repository: https://github.com/Jamesbarford/flat-group
- Homepage: https://github.com/Jamesbarford/flat-group#readme
- Issues: https://github.com/Jamesbarford/flat-group/issues
- npm.io page: https://npm.io/package/flat-group

## Recent versions

- 0.0.9 (latest) — 2020-07-23
- 0.0.8 — 2020-07-22
- 0.0.7 — 2020-07-22
- 0.0.6 — 2020-07-20
- 0.0.5 — 2020-07-20
- 0.0.4 — 2020-07-18
- 0.0.3 — 2020-07-18
- 0.0.2 — 2020-07-18
- 0.0.1 — 2020-07-18

## README

# Flat Group

The purpose of this small module is to make an array of json grouped and thus easier to query as a group like structure. Something which is useful for libraries like `D3.js`.

## How to import:

```js
const flatGroup = require("flat-group");

// or 

import flatGroup from "flat-group";
```

**example 1**

For demonstrative purposes this illustrates nothing particularly exciting with just one key 

```js
const arr = [
    { id: 1, group_1: true, group_2: "red" },
    { id: 2, group_1: true, group_2: "yellow" },
    { id: 3, group_1: false, group_2: "yellow" },
    { id: 4, group_1: true, group_2: "green" },
    { id: 5, group_1: false, group_2: "green" },
    { id: 6, group_1: true, group_2: "red" },
]

const groups = flatGroup(arr, ["group_1"]);
```

returns: 

```json
{
    "group_1": {
        "keys": [
            true,
            false
        ],
        "true": [
            {
                "id": 1,
                "group_1": true,
                "group_2": "red"
            },
            {
                "id": 2,
                "group_1": true,
                "group_2": "yellow"
            },
            {
                "id": 4,
                "group_1": true,
                "group_2": "green"
            },
            {
                "id": 6,
                "group_1": true,
                "group_2": "red"
            }
        ],
        "false": [
            {
                "id": 3,
                "group_1": false,
                "group_2": "yellow"
            },
            {
                "id": 5,
                "group_1": false,
                "group_2": "green"
            }
        ]
    }
}
```

**example 2** 

The second argument keys can support `n` number of keys meaning you can cluster your data into n number of groups:

using the same array from above:

```js
const groups = flatGroup(arr, ["group_1", "group_2"]);

// returns

{
    "group_1": {
        "keys": [
            true,
            false
        ],
        "true": [
            {
                "id": 1,
                "group_1": true,
                "group_2": "red"
            },
            {
                "id": 2,
                "group_1": true,
                "group_2": "yellow"
            },
            {
                "id": 4,
                "group_1": true,
                "group_2": "green"
            },
            {
                "id": 6,
                "group_1": true,
                "group_2": "red"
            }
        ],
        "false": [
            {
                "id": 3,
                "group_1": false,
                "group_2": "yellow"
            },
            {
                "id": 5,
                "group_1": false,
                "group_2": "green"
            }
        ]
    },
    "group_2": {
        "keys": [
            "red",
            "yellow",
            "green"
        ],
        "red": [
            {
                "id": 1,
                "group_1": true,
                "group_2": "red"
            },
            {
                "id": 6,
                "group_1": true,
                "group_2": "red"
            }
        ],
        "yellow": [
            {
                "id": 2,
                "group_1": true,
                "group_2": "yellow"
            },
            {
                "id": 3,
                "group_1": false,
                "group_2": "yellow"
            }
        ],
        "green": [
            {
                "id": 4,
                "group_1": true,
                "group_2": "green"
            },
            {
                "id": 5,
                "group_1": false,
                "group_2": "green"
            }
        ]
    }
}
```

---
_Source: https://npm.io/package/flat-group · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
