Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fulcrum

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fulcrum

Array pivot table

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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();

FAQs

Package last updated on 13 Mar 2014

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc