1.1.0 • Published 8 years ago

dictify v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

dictify

npm version Build Status

Convert an object-list to dictionary by specified key as an index

Installation

npm install --save dictify

Example

var assert = require('assert');
var dictify = require('dictify');

var objectList = [
  { x: 'foo' },
  { x: 'bar' },
  { x: 'baz' },
];

var dict = dictify(objectList, 'x');
assert.deepEqual(
  dict,
  {
    foo: { x: 'foo' },
    bar: { x: 'bar' },
    baz: { x: 'baz' },
  }
);

var dictByFunctionIndexer = dictify(objectList, function(obj) {
  return obj.x.toUpperCase();
});
assert.deepEqual(
  dictByFunctionIndexer,
  {
    FOO: { x: 'foo' },
    BAR: { x: 'bar' },
    BAZ: { x: 'baz' },
  }
);