to-object-reducer

1.0.1 • Public • Published

to-object-reducer

Build Status

Simple JavaScript/Node.js module which exports a toObject reducer that can be used with arr.reduce(toObject) to convert an array [[key,value],...] to an object { key: value, ... }.

Example:

[['username', 'olalonde'], ['id',1337]].reduce(toObj, {})
// => { username: 'olalonde', id: 1337 }

Install

npm install --save to-object-reducer

Usage

It is meant to facilitate the pattern of converting an object to an array of [key,val] for easier manipulation with Array methods and back to an object, e.g.:

const toObject = require('to-object-reducer')
 
// Lower case object keys
const lowerCaseHeaders = (headers = {}) => Object
  .keys(headers)
  .map(key => [key, headers[key]])
  .map([key, val] => [key.toLowerCase(), val])
  .reduce(toObject, {})
 
// Or with ES2017 Object.entries
const lowerCaseHeaders = (headers = {}) => Object
  .entries(headers)
  .map([key, val] => [key.toLowerCase(), val])
  .reduce(toObject, {})

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i to-object-reducer

Weekly Downloads

178

Version

1.0.1

License

MIT

Last publish

Collaborators

  • olalonde