Construct an object from an array of objects.
Installation
$ npm install index-array
Usage
indexArray(array, key, [options])
- the input array must be an array of objects
- key is a single string corresponding to a key, in a key/value pair present in every object in the array
- there is an optional options object with the following choices (
{arraify: true}
,{overwrite: true}
)
var indexArray = var data = id: 'e6a7f89' name: 'Joe Bloggs' email: 'joe@bloggs.com' id: '300e9ea' name: 'John Doe' email: 'john@doe.com' // => {// 'Joe Bloggs': {id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'},// 'John Doe': {id: '300e9ea', name: 'John Doe', email: 'john@doe.com'}// } // => {// 'Joe Bloggs': [{id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'}],// 'John Doe': [{id: '300e9ea', name: 'John Doe', email: 'john@doe.com'}]// } // add a similar element to the array (in this case the `id` is different) data // by default the `John Doe` key already populated doesn't get overwritten // => {// 'Joe Bloggs': {id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'},// 'John Doe': {id: '300e9ea', name: 'John Doe', email: 'john@doe.com'}// } // but if `{arraify: true}` the similar element gets pushed to the array // => {// 'Joe Bloggs': [{id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'}],// 'John Doe': [// {id: '300e9ea', name: 'John Doe', email: 'john@doe.com'},// {id: 'a6a78ee', name: 'John Doe', email: 'john@doe.com'}// ]// } // if not using `arraify`, you can supply `{overwrite: true}` // now the extra pushed element overwrites the first // => {// 'Joe Bloggs': {id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'},// 'John Doe': {id: 'a6a78ee', name: 'John Doe', email: 'john@doe.com'}// }
License
MIT