Giter Site home page Giter Site logo

json.pivot's Introduction

json.pivot

*What:* 
A JavaScript function for performing "pivot table"-like operations on JSON data.

*How:* 
call pivot(data, rowNames, columnNames, userOptions)
  data - an array of *homogeneous json objects. (* at least share some common fields that we're interested in)
  rowNames - an array of property names, each one is a property of all objects in data 
  colNames - an array of property names, each one is a property of all objects in data 
  (colNames and rowNames should not contain any similar values)
  userOptions - to be documented later. leave it null or peek at the code


Example:
var data = [
  {subject : 1, day : 1, value : 31.25, treatment : 'A' },
  {subject : 1, day : 2, value : 31.25, treatment : 'C' },
  {subject : 1, day : 3, value : 33.12, treatment : 'B' },
  {subject : 2, day : 1, value : 25.87, treatment : 'C' },
  {subject : 2, day : 2, value : 26.63, treatment : 'A' },
  {subject : 2, day : 3, value : 26.00, treatment : 'B' },
  {subject : 3, day : 1, value : 23.75, treatment : 'C' },
  {subject : 3, day : 2, value : 26.13, treatment : 'B' },
  {subject : 3, day : 3, value : 24.87, treatment : 'A' },
  {subject : 4, day : 1, value : 28.75, treatment : 'A' },
  {subject : 4, day : 2, value : 29.63, treatment : 'B' },
  {subject : 4, day : 3, value : 29.87, treatment : 'C' },
  {subject : 5, day : 1, value : 24.50, treatment : 'C' },
  {subject : 5, day : 2, value : 28.63, treatment : 'A' },
  {subject : 5, day : 3, value : 28.37, treatment : 'B' },
  {subject : 6, day : 1, value : 31.25, treatment : 'B' },
  {subject : 6, day : 2, value : 30.63, treatment : 'A' },
  {subject : 6, day : 3, value : 29.37, treatment : 'C' },
  {subject : 7, day : 1, value : 25.50, treatment : 'B' },
  {subject : 7, day : 2, value : 23.87, treatment : 'C' },
  {subject : 7, day : 3, value : 24.00, treatment : 'A' },
  {subject : 8, day : 1, value : 28.50, treatment : 'B' },
  {subject : 8, day : 2, value : 27.87, treatment : 'C' },
  {subject : 8, day : 3, value : 30.12, treatment : 'A' },
  {subject : 9, day : 1, value : 25.13, treatment : 'A' },
  {subject : 9, day : 2, value : 27.00, treatment : 'B' },
  {subject : 9, day : 3, value : 24.63, treatment : 'C' }
];

var result = pivot(data, ['subject'], ['day','treatment'], {});

result is an array of rows, of columns of data.
In this case, rows are sorted by subject, columns are sorted by day, then treatment.
result.rowHeaders containes information about the row headers.
result.columnHeaders containes information about the column headers.

result[row][column] will return undefined if no data exists in that slot,
otherwise it will contain an array of all data that fell in that slot.

Something like this is perfectly legal too:
var result = pivot(data, ['treatment'], [], {});

It aggregates/sorts the data based on treatment.

-Mark Bolusmjak
[email protected]

json.pivot's People

Watchers

Kathy Doody avatar James Cloos 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.