Giter VIP home page Giter VIP logo

array-groups's Introduction

Build StatusCommitizen friendlyConventional CommitsBugsCode SmellsDuplicated Lines (%)Vulnerabilities

Sonar Cloud

Quality Gate StatusCoveragenpmversion@masterDownloads@YearGitHub issues

array-groups

Tool to organize arrays of objects in sub-arrays grouped by value-ranges that is near a central value provided

Installation

npm install array-groups

Usage

array-groups can group any array of objects that have a numeric field in common. array-groups receives the main array, which contains the objects, and also checks an auxiliary array, which has the pure numeric values, in the same order as the array of objects, and also receives a value specifying a grouping range.

Example:

const group = require(array-groups);

let t1 = {
    value: 5,
    field: 'testing text',

}

let t2 = {
    value: 7,
    field: 'testing text',

}

let t3 = {
    value: 4,
    field: 'testing text',

}

let t4 = {
    value: 25,
    field: 'testing text',
    field2: 22
}

let t5 = {
    value: 21,
    field: 'testing text',

}

let t6 = {
    value: 44,
    field: 'testing text',

}

let t7 = {
    value: 48,
    field: 'testing text',

}

let t8 = {
    value: 55,
    field: 'testing text',

}

let t9 = {
    value: 59,
    field: 'testing text',

}

let t10 = {
    value: 50,
    field: 'testing text',

}

let testingArray = [ t1, t2, t3, t4, t5, t6, t7, t8, t9, t10 ]; // this is our Object Array that will be grouped in sub-arrays.

let valuesArray = []; // this is a numeric array containing the values to be grouped in the objects array as follows:

for ( let i = 0; i < testingArray.length; i++ ) {
    valuesArray.push( testingArray[ i ].value );
}

let range = 10; // grouping range


let grouped = group.GroupArray( testingArray, valuesArray, range );

That code should return a grouped array, that contain the sub-arrays already grouped. the grouped var should have this value:

[ 
    [   { value: 5, field: 'testing text' },
        { value: 7, field: 'testing text' },
        { value: 4, field: 'testing text' } 
    ],
    [ 
        { value: 25, field: 'testing text', field2: 22 },
        { value: 21, field: 'testing text' } 
    ],
    [ 
        { value: 44, field: 'testing text' },
        { value: 48, field: 'testing text' },
        { value: 55, field: 'testing text' },
        { value: 59, field: 'testing text' },
        { value: 50, field: 'testing text' } 
    ] 
]

Testing

run the unit test:

npm test

array-groups's People

Contributors

naturesprophet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.