# index-array

> construct an object from an array of objects

Latest version **4.0.0** (published 2015-12-02) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install index-array
pnpm add index-array
yarn add index-array
bun add index-array
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2015-12-02 |
| First published | 2015-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Rory Bradford |
| Maintainers | roryrjb |
| Keywords | array, objects, object |

## Links

- npm: https://www.npmjs.com/package/index-array
- Repository: https://github.com/roryrjb/index-array
- Issues: https://github.com/roryrjb/index-array/issues
- npm.io page: https://npm.io/package/index-array

## Recent versions

- 4.0.0 (latest) — 2015-12-02
- 3.1.0 — 2015-07-02
- 3.0.0 — 2015-07-02
- 2.1.0 — 2015-06-21
- 2.0.0 — 2015-06-09
- 1.1.0 — 2015-06-09
- 1.0.0 — 2015-02-23

## README

# index-array [![Build Status](https://travis-ci.org/roryrjb/index-array.svg?branch=master)](https://travis-ci.org/roryrjb/index-array) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)

> 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}`)

```javascript
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

---
_Source: https://npm.io/package/index-array · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
