Giter VIP home page Giter VIP logo

json-groupby's Introduction

json-groupby

Group array of JSON objects based on associated properties.

It also groups objects containing nested arrays.

installation

npm install json-groupby

usage

var groupBy = require('json-groupby')
var group = groupBy(array, properties [, collect])
  • array Array of JSON objects

  • properties Array JSON properties' path like address.city or lookup object

    lookup

    {
      intervals: array of numbers
      ,property: string
      [,labels: array of string]
    }
    

    intervals Array of intervals. Like [ 10, 20, 30, 40, 50] group the data in four ranges, whereas lower bound is inclusive and upper bound is exclusive.

    peroperty Property path like price

    labels Array of interval labels like [ 'low', 'medium', 'high']

  • collect Array of properties that need to be collected in array

examples

data set

var products = 
 [{"id": 1,
   "product": "ri", "price": 16, "color": "green", "available": false,
   "tags": ["bravo"],
   "vendor": {"name": "Donald Chambers", "address": {"city": "Mumbai"}}},
  {"id": 2,
   "product": "foef", "price": 44, "color": "yellow", "available": false,
   "tags": ["alpha"],
   "vendor": {"name": "Barbara Garrett", "address": {"city": "Mumbai"}}},
  {"id": 3,
   "product": "jehnojto", "price": 29, "color": "red", "available": true,
   "tags": ["alpha"],
   "vendor": {"name": "Anne Leonard", "address": {"city": "New York"}}},
  {"id": 4,
   "product": "ru", "price": 35, "color": "yellow", "available": false,
   "tags": ["echo", "charlie", "bravo"],
   "vendor": {"name": "Justin Doyle", "address": {"city": "London"}}},
  {"id": 5,
   "product": "pihluve", "price": 47, "color": "green", "available": true,
   "tags": ["delta", "echo", "bravo"],
   "vendor": {"name": "Emily Abbott", "address": {"city": "New York"}}},
  {"id": 6,
   "product": "dum", "price": 28, "color": "green", "available": true,
   "tags": ["echo", "delta", "charlie"],
   "vendor": {"name": "Henry Peterson", "address": {"city": "New York"}}},
  {"id": 7,
   "product": "zifpeza", "price": 10, "color": "green", "available": false,
   "tags": ["echo", "charlie", "bravo"],
   "vendor": {"name": "Jesus Lowe", "address": {"city": "Mumbai"}}},
  {"id": 8,
   "product": "av", "price": 39, "color": "green", "available": true,
   "tags": ["bravo"],
   "vendor": {"name": "Rosalie Erickson", "address": {"city": "New York"}}}]

group by single property

groupBy(products, ['color'], ['id'])
// output is 
{ green: { id: [ 1, 5, 6, 7, 8 ] },
  yellow: { id: [ 2, 4 ] },
  red: { id: [ 3 ] } }

group by many properties and without collect option

groupBy(products, ['available', 'color', 'vendor.address.city'])
// output is 
{"false": 
  {"green": 
    {"Mumbai": [
      {"id": 1, "product": "ri", "price": 16, "color": "green", 
       "available": false, "tags": ["bravo"], 
       "vendor": {"name": "Donald Chambers",  "address": {"city": "Mumbai"}}},
      {"id": 7, "product": "zifpeza", "price": 10, "color": "green",
       "available": false, "tags": ["echo", "charlie", "bravo"],
       "vendor": {"name": "Jesus Lowe", "address": {"city": "Mumbai"}}}]},
   "yellow": {
     "Mumbai": [
       {"id": 2, "product": "foef", "price": 44, "color": "yellow", 
        "available": false, "tags": ["alpha"], 
        "vendor": {"name": "Barbara Garrett",  "address": {"city": "Mumbai"}}}], 
     "London": [
       {"id": 4, "product": "ru", "price": 35, "color": "yellow",
        "available": false, "tags": ["echo", "charlie", "bravo"],
        "vendor": {"name": "Justin Doyle", "address": {"city": "London"}}}]}},
 "true": 
  {"red": 
    {"New York": [
      {
        "id": 3, "product": "jehnojto", "price": 29, "color": "red",
        "available": true, "tags": ["alpha"],
        "vendor": {"name": "Anne Leonard", "address": {"city": "New York"}}}]},
   "green": {
     "New York": [
        {"id": 5, "product": "pihluve", "price": 47, "color": "green",
         "available": true, "tags": ["delta", "echo", "bravo"],
         "vendor": {"name": "Emily Abbott", "address": {"city": "New York"}}},
         {"id": 6, "product": "dum", "price": 28, "color": "green",
         "available": true, "tags": ["echo", "delta", "charlie"],
         "vendor": {"name": "Henry Peterson", "address": {"city": "New York"}}},
         {"id": 8, "product": "av", "price": 39, "color": "green",
         "available": true, "tags": ["bravo"],
         "vendor": {"name": "Rosalie Erickson", "address": {"city": "New York"}}}
     ]}}}

single deep path property

groupBy(products, ['vendor.address.city'], ['id'])
// output is 
{ Mumbai: { id: [ 1, 2, 7 ] },
  'New York': { id: [ 3, 5, 6, 8 ] },
  London: { id: [ 4 ] } }

group with boolean property

groupBy(products, ['available'], ['id'])
// output is 
{ false: { id: [ 1, 2, 4, 7 ] }, 
  true: { id: [ 3, 5, 6, 8 ] }}

group by intervals (lookup of intervals) without intervals' name

groupBy(
  products, 
  [{property: 'price', intervals: [10,20,40,50]}],
  ['id'])
//output is 
{ '0': { id: [ 1, 7 ] },
  '1': { id: [ 3, 4, 6, 8 ] },
  '2': { id: [ 2, 5 ] } }

group by intervals (lookup of intervals) with intervals' lable name

groupBy(
  products, 
  [{
    property: 'price', 
    intervals: [10,20,40,50], 
    labels: ['low','medium','high']}],
  ['id'])
//ouptu is 
{'low': { id: [ 1, 7 ] },
 'medium': { id: [ 3, 4, 6, 8 ] },
 'high': { id: [ 2, 5 ] } }

group with mixed properties lookup and property path

groupBy(
  products, 
  [
    {
      property: 'price', 
      intervals: [10,20,40,50], 
      labels: ['low','medium','high']
    },
    'vendor.address.city'
  ],
  ['id'])
// output is
{
  "low":
    {"Mumbai":{"id":[1,7]}},
  "high":
    {"Mumbai":{"id":[2]},
    "New York":{"id":[5]}},
  "medium":
    {"New York":{"id":[3,6,8]},
    "London":{"id":[4]}}

group by tags that are in array

groupBy(products, ['tags'], ['id'])
//ouput is
{ bravo: { id: [ 1, 4, 5, 7, 8 ] },
  alpha: { id: [ 2, 3 ] },
  echo: { id: [ 4, 5, 6, 7 ] },
  charlie: { id: [ 4, 6, 7 ] },
  delta: { id: [ 5, 6 ] } }

group and collect many properties

groupBy(
  products, 
  ['color'], 
  ['vendor.address.city', 'available'])
// output is
{ green: 
   { 'vendor.address.city': [ 'Mumbai', 'New York', 'New York', 'Mumbai', 'New York' ],
     available: [ false, true, true, false, true ] },
  yellow: 
   { 'vendor.address.city': [ 'Mumbai', 'London' ],
     available: [ false, false ] },
  red: { 'vendor.address.city': [ 'New York' ], available: [ true ] } }

Nested Arrays

Group by property path that lies in nested arrays, in the following example addresses.localities.size

var vendors = [{
  id: 1,
  addresses : [{
    city: 'a',
    localities: [
      {size: "small", zipcode: '12345', storeType: ['electronic', 'food']},
      {size: "medium", zipcode: '12346', storeType: ['food']}]
  }, {
    city: 'b',
    localities: [
      {size: "medium", zipcode: '12345', storeType: ['electronic', 'food']},
      {size: "small", zipcode: '12347', storeType: ['electronic']}]
  }],
  details: {
    name: 'foo', 
    items: 400, 
    rating: 'high'}
}, {
  id: 2,
  addresses : [{
    city: 'a',
    localities: [
      {size: "large", zipcode: '12345', storeType: ['apparel', 'furniture']},
      {size: "small", zipcode: '12346', storeType: ['furniture']}]
  }, {
    city: 'b',
    localities: [
      {size: "small", zipcode: '12345', storeType: ['food', 'furniture']},
      {size: "medium", zipcode: '12347', storeType: ['food']}]
  }],
  details: {
    name: 'bar', 
    items: 500, 
    rating: 'low'}
}]

var group = groupBy(vendors, ['addresses.localities.size'], ['id'])

// output gruop is
{
  "small": {id: [1, 2]},
  "medium": {id: [1, 2]},
  "large": {id: [2]}
}

developing

Once you run

npm install

then for running test

npm run test

to create build

npm run build

license

This project is licensed under the terms of the MIT license.

json-groupby's People

Contributors

gagan-bansal avatar prashantswami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-groupby's Issues

[feature request] nested array

This library is very useful and the syntax make it easy to understand (very close to pandas a Python data manipulation library). However I see a missing feature that concern recurring use case .

Given the example of a book store, I retrieve the list of books with their title and the list of their authors.

  {
    "title": "",
    "author": [
      {
        "given": "",
        "family": "",
        "affiliation": []
      },
      {
        "given": "",
        "family": "",
        "affiliation": []
      },
]

Then I want to group those books by authors. (This append a lot when you have entities with a relation where books asMany authors.

Your library could implement a SQL like syntax to specify on which property inside the array you want to group

groupBy(books, ['author.given'])

One solution to tackle this would be to add a temporary array tmp that contain the property given for each author then use the group by as for tags

  books.forEach(function(item) {
      item.tmp = item['authors'].map(function(x) {
         return x.['given'];
      })
  });

groupBy(books, ['tmp'])

I believe this feature will complete the coverage of this library in terms of grouping and manipulating JSON Objects

json groupBy transform a key into array

Hi,

Thanks for this package, I using it and liked.
I have a problem with a json here:

[
    {
        "id_programacao": 1,
        "evento": "TESTE PROGRAMACAO 1",
        "descricao_evento": "teste usando o NodeJS",
        "tipo_evento": "Palestra",
        "local": "PRODAM",
        "hora_inicio": "2016-10-10T22:26:15.000Z",
        "hora_fim": "2016-10-13T22:26:22.000Z",
        "id_palestrante": 2,
        "nome": "Bruno Mendes",
        "cargo": "Analista",
        "empresa": "PRODAM",
        "curriculum": "Tem muito que aprender ainda... ¬¬"
    },
    {
        "id_programacao": 1,
        "evento": "TESTE PROGRAMACAO 1",
        "descricao_evento": "teste usando o NodeJS",
        "tipo_evento": "Palestra",
        "local": "PRODAM",
        "hora_inicio": "2016-10-10T22:26:15.000Z",
        "hora_fim": "2016-10-13T22:26:22.000Z",
        "id_palestrante": 1,
        "nome": "Rodolfo Azevedo",
        "cargo": "Programador",
        "empresa": "PRODAM",
        "curriculum": "Esse bixo manja velho"
    },
    {
        "id_programacao": 2,
        "evento": "TESTE PROGRAMACAO 2",
        "descricao_evento": "teste usando o Java",
        "tipo_evento": "Palestra",
        "local": "PRODAM",
        "hora_inicio": "2016-10-14T22:26:15.000Z",
        "hora_fim": "2016-10-15T22:26:22.000Z",
        "id_palestrante": 3,
        "nome": "Bruno do Mobile",
        "cargo": "Programador",
        "empresa": "PRODAM",
        "curriculum": "Até nos feriados"
    }
]

I have id_programacao: 1, but I have two id_palestrantes in this programacao, I like to transform this json to this:

[
    {
        "id_programacao": 1,
        "evento": "TESTE PROGRAMACAO 1",
        "descricao_evento": "teste usando o NodeJS",
        "tipo_evento": "Palestra",
        "local": "PRODAM",
        "hora_inicio": "2016-10-10T22:26:15.000Z",
        "hora_fim": "2016-10-13T22:26:22.000Z",
        palestrantes: [
          {
             "id_palestrante": 2,
             "nome": "Bruno Mendes",
             "cargo": "Analista",
             "empresa": "PRODAM",
             "curriculum": "Tem muito que aprender ainda... ¬¬"
          },
          {
             "id_palestrante": 1,
             "nome": "Rodolfo Azevedo",
             "cargo": "Programador",
             "empresa": "PRODAM",
             "curriculum": "Esse bixo manja velho"
          }
       ]
    },
    {
        "id_programacao": 2,
        "evento": "TESTE PROGRAMACAO 2",
        "descricao_evento": "teste usando o Java",
        "tipo_evento": "Palestra",
        "local": "PRODAM",
        "hora_inicio": "2016-10-14T22:26:15.000Z",
        "hora_fim": "2016-10-15T22:26:22.000Z",
        "id_palestrante": 3,
        "nome": "Bruno do Mobile",
        "cargo": "Programador",
        "empresa": "PRODAM",
        "curriculum": "Até nos feriados"
    }
]

Can you help me ?

Thanks and Regards.

properties can be a single string

Convention over configuration.

var group = groupBy(array, properties [, collect])

Right now properties is an array, I think can be a string in case of single property.

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.