This package has been deprecated

Author message:

Development of this module has been stopped.

index-array

4.0.0 • Public • Published

index-array Build Status js-standard-style

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 = require('index-array')
 
var data = [
  {id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'},
  {id: '300e9ea', name: 'John Doe', email: 'john@doe.com'}
]
 
indexArray(data, 'name')
 
// => {
//   'Joe Bloggs': {id: 'e6a7f89', name: 'Joe Bloggs', email: 'joe@bloggs.com'},
//   'John Doe': {id: '300e9ea', name: 'John Doe', email: 'john@doe.com'}
// }
 
indexArray(data, 'name', {arraify: true})
 
// => {
//   '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.push({id: 'a6a78ee', name: 'John Doe', email: 'john@doe.com'})
 
indexArray(data, 'name')
 
// 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}`
 
indexArray(data, 'name', {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

Readme

Keywords

Package Sidebar

Install

npm i index-array

Weekly Downloads

7

Version

4.0.0

License

MIT

Last publish

Collaborators

  • roryrjb