Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kjirou/dictify

Convert an object-list to dictionary by specified key as an index
https://github.com/kjirou/dictify

Last synced: 12 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# dictify

[![npm version](https://badge.fury.io/js/dictify.svg)](http://badge.fury.io/js/dictify)
[![Build Status](https://travis-ci.org/kjirou/dictify.svg?branch=master)](https://travis-ci.org/kjirou/dictify)

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

## Installation

```bash
npm install --save dictify
```

## Example

```js
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' },
}
);
```