objectify-array
Recursively convert arrays of objects into a single keyed object tree
Installation
npm install objectify-array --save
or
yarn add objectify-array
Usage
const objectifyArray = var people = id: 'bob' fullName: 'Bob Bobberson' id: 'sue' fullName: 'Sue Zinn' id: 'hal' fullName: 'Hal Itosis' var peopleMap =
This will return an object, using the id
property values as keys:
bob: id: 'bob' fullName: 'Bob Bobberson' sue: id: 'sue' fullName: 'Sue Zinn' hal: id: 'hal' fullName: 'Hal Itosis'
id
is the default property name to index by. If you want to use another property,
specify it as an optional second argument:
var peopleMap =
You can also specify multiple key names as an array, which are tried in the order you provide them.
This is most useful when you use the recursive
option:
var todos = id: 10 description: 'Learn Things' tags: name: 'a' score: 15 name: 'b' score: 83 id: 20 description: 'Do Things' tags: name: 'x' score: 3 name: 'y' score: 9 var todosMap = todosMap10 //=> { id: 10, description: ..., tags: ... }todosMap20 //=> { id: 10, description: ..., tags: ... } todosMap10tagsascore //=> 15todosMap10tagsbscore //=> 83
you can specify options.by
to be a function, this function will receive the element and should return the key to be used for this element.
var users = id: 'u' name: 'Alice' hash: 22 var peopleMap = peopleMapu_22name //=> Alice
For more extensive usage examples, see test.js
API
The module exports a single function:
objectifyArray(array[, options])
array
Array - The array of objects to index.options
Object (optional) -{ by: String | Array<String>, recursive: Boolean }
by
(default:['id']
) - The key (String) or keys (Array of Strings) to index by.recursive
(default:false
) - If true, will deeply objectify all arrays in your array of objects.
Tests
npm i && npm t
Dependencies
None
Dev Dependencies
- standard: JavaScript Standard Style
- tap-spec: Formatted TAP output like Mocha's spec reporter
- tape: tap-producing test harness for node and browsers
License
MIT