Git Product home page Git Product logo

fulcrum's Introduction

Array Pivot

A utility to generate pivot tables from an array of objects:

var pivot = require('fulcrum');

var data = [{
	name: 'John',
	coin: '5ct',
	number: 30
},{
	name: 'John',
	coin: '5ct',
	number: 2
},{
	name: 'John',
	coin: '10ct',
	number: 3
},{
	name: 'Jane',
	coin: '10ct',
	number: 15
},{
	name: 'Jane',
	coin: '5ct',
	number: 6
}];

In this data model, John has 32 5ct and 3 10ct coins, Jane has 6 5ct and 15 10ct coins.

var res = pivot(data,'name','coin',pivot.sum('number'));

[{
    "name": "John",
    "5ct": 32,
    "10ct": 3
},{
    "name": "Jane",
    "5ct": 6,
    "10ct": 15
}]

API

pivot(array,rows,cols,reducer);
  • array: an array of objects

  • rows: a string for the field for rows

  • cols: a string for the field for columns

  • reducer (optional): an object describing how to calculate the cell value from the matched objects:

      {
      	callback: Function,
      	initialValue: Object
      }
    

    These are the same arguments as passed into Array.reduce: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce.

    If the reducer is omitted, an array of objects is returned in the cell

Built in reducers

  • pivot.sum(field): return the sum of the values in field field
  • pivot.count(): return the number of objects in the cell
  • pivot.average(field): return the average
  • pivot.min(field): return the minimum
  • pivot.max(field): return the maximum

For all these reducers, when no objects are present in the cell, null is returned, except for count()

Convenience attachments to Array.prototype

If you wish, you can attach a convenience method to Array.prototype:

var pivot = require('fulcrum').attach();
var res = data.pivot('name','coin',pivot.sum('number'));

It can also be detached:

pivot.detach();

fulcrum's People

Watchers

Rikkert Koppes 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.