Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/kjirou/dictify
- Owner: kjirou
- License: mit
- Created: 2015-10-05T17:38:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T07:16:59.000Z (about 3 years ago)
- Last Synced: 2024-10-26T09:09:58.725Z (13 days ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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' },
}
);
```