New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

flat-group

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flat-group

A function for creating flat groups from items in array


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created

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:

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

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:

{
    "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:

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"
            }
        ]
    }
}

FAQs

Package last updated on 23 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts