pivot JSON key / value array properties to actual objects
npm install pivot-keyvaluespivot an array of keys and value entries into actual objects.
Example: [{name: 'url', value: 'https://d3js.org'}] → {url: 'https://d3js.org'}
Converting arrays of keys / values into objects has the following benefits:
* values can be looked up by key, instead of having to iterate through the array (likely multiple times).
* the data structure is more compact.
npm install pivot-keyvalues
#### Parameters
* data object containing an array of data to be pivoted - either at root level, or nested.
* options object (optional):
* keyName string - default to 'name'
* valueName string - default to 'value'
#### Return
Original object with transposed data structure: array is transformed into an object
```javascript
const pivot = require('pivot-keyvalues')
let dataset = [
{
name: 'website'
, value: 'https://d3js.org/'
}
, {
name: 'visitors'
, value: 10000
}
]
pivot(dataset)
// Returns
//
// {
// website: 'https://d3js.org/'
// , visitors: 10000
// }
``
``javascript
let dataset = [
{
title: 'website'
, data: 'https://stackoverflow.com/'
}
, {
title: 'visitors'
, data: 500000
}
]
pivot(dataset, { keyName: 'title', valueName: 'data' })
// Returns
//
// {
// website: 'https://stackoverflow.com/'
// , visitors: 500000
// }
```
New releases are published on npm whenever a new tag is pushed on github (via travis-ci).