1.3.1 • Published 1 year ago

pivot-table-js v1.3.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

build code-size min-size types node npm

pivot-table-js

A lightweight module that takes an array of objects and produces an array of objects back based on one or more aggregate function per column. Emulating excel pivot tables.

pivot-table-js can calculate different aggregate functions on sets of values. The results can be optionally renamed.

Install

Using npm:

$ npm install pivot-table-js

Using yarn:

$ yarn add pivot-table-js

Example

import { Pivot } from 'pivot-table-js'


const data = [
  {
    domain: 'duckduckgo.com',
    path: '/search',
    traffic: 15000,
    trustFlow: 30
  },
  {
    domain: 'duckduckgo.com',
    path: '/images',
    traffic: 8000,
    trustFlow: 20
  },
  {
    domain: 'google.com',
    path: '/search',
    traffic: 20000,
    trustFlow: 42
  },
  {
    domain: 'google.com',
    path: '/images',
    traffic: 10000,
    trustFlow: 38
  }
]


const index = 'domain'

const aggFunc =   {
  domain: 'count', 
  traffic: ['sum', 'mean'], 
  trustFlow: 'mean' 
}

const rename = ['Domain', 'Frequency', 'Traffic Sum', 'Traffic Average', 'TF Average']

const pivotTable = Pivot(data, index, aggFunc, rename)

console.log(pivotTable)

Will output:

[{
  Domain: 'duckduckgo.com',
  'Frequency': 2,
  'Traffic Sum': 23000,
  'Traffic Average': 11500,
  'TF Average': 25
},
{
  Domain: 'google.com',
  'Frequency': 2,
  'Traffic Sum': 30000,
  'Traffic Average': 15000,
  'TF Average': 40
},
{
  Domain: 'Grand Total',
  'Frequency': 4,
  'Traffic Sum': 53000,
  'Traffic Average': 13250,
  'TF Average': 32.5
}]
DomainFrequencyTraffic SumTraffic AverageAverage TF
duckduckgo.com2230001150025
google.com2300001500040
Grand Total4530001325032.5

Updates

New feature allows for multiple funcions on the same column, just enclose the type of funcions in an array

const aggFunc =   {
  domain: 'count', 
  traffic: ['sum', 'mean'], 
  trustFlow: 'mean' 
}

Available aggregate functions

FunctionDefinition
countCalculates the count of all values in a set
countaCalculates the count of all values in a set including empty strings
count-uniqueCalculates the count of all unique values in a set
sumCalculates the sum of values.
meanCalculates the average in a set of values — not rounded
medianCalculates the median in a set of values — not rounded
modeCalculates the mode in a set of values
minMinimum gets the minimum value in a set of values
maxMaximun gets the maximum value in a set of values

Usage

Pivot(data, index, values ,rename)

  • data <Array<Object>> Prepared Array of objects to pivot against.
  • index <string> The index row to use as pivot.
  • values <Object> Aggregate functions
    • [column: <string>]: <Array<string>> | string Use array for more than one option on the same column
  • rename <Array<string>> Optionally rename the output columns, the order is important.
  • returns: <Array<Object>>

1.3.1

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago