# @juliancoleman/assoc-by-key

> Converts an object of objects into an array of objects with a given predicate.

Latest version **0.0.2** (published 2017-10-31) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @juliancoleman/assoc-by-key
pnpm add @juliancoleman/assoc-by-key
yarn add @juliancoleman/assoc-by-key
bun add @juliancoleman/assoc-by-key
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2017-10-31 |
| First published | 2017-10-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Julian Coleman |
| Maintainers | juliancoleman |

## Links

- npm: https://www.npmjs.com/package/@juliancoleman/assoc-by-key
- npm.io page: https://npm.io/package/@juliancoleman/assoc-by-key

## Dependencies (2)

- [ramda](https://npm.io/package/ramda.md) ^0.25.0
- [lcov-parse](https://npm.io/package/lcov-parse.md) ^1.0.0

## Recent versions

- 0.0.2 (latest) — 2017-10-31
- 0.0.1 — 2017-10-25

## README

# assoc-by-key

[![Build Status](https://semaphoreci.com/api/v1/juliancoleman/assoc-by-key/branches/master/badge.svg)](https://semaphoreci.com/juliancoleman/assoc-by-key)
[![npm version](https://badge.fury.io/js/%40juliancoleman%2Fassoc-by-key.svg)](https://badge.fury.io/js/%40juliancoleman%2Fassoc-by-key)

A curried function using Ramda that transforms an object of
objects into an array of objects, where the key becomes a
property of that object. The function will return a new
array and will not mutate or destroy the original object.

This function performs the inverse of [index-by-key](https://github.com/juliancoleman/)

## Install

### `yarn`

```sh
yarn add @juliancoleman/assoc-by-key
```

### `npm`

```sh
npm i -S @juliancoleman/assoc-by-key
```

### Setup

This package provides the single function mentioned above.
You can specify the key on `require`, or you can specify a
key to `assoc` by later on.

```js
// initialize without specified key
const assocByKey = require("@juliancoleman/assoc-by-key");

// initialize with specified key at require
const assocById = require("@juliancoleman/assoc-by-key")("id");

// initialize with specified key later
const assocByFirstName = assocByKey("first_name");
```

Once required and a key is specified, you can then call the
function on your data to see the transformation. Again, the
function will return a new array and will not mutate or
destroy the original object.

Below is an example use on a `Promise` object returned by a
database call:

```js
const assocByKey = require("@juliancoleman/assoc-by-key");

const { getUsers } = appRequire("path/to/API");

(async () => {
  const assocById = assocByKey("id");
  const users = await getUsers();

  /*
  Example `users`

    { "1": { "first_name": "Julian",
              "last_name": "Coleman",
              "email_address": "julcol03@gmail.com"
            },
      "2": { "first_name": "Bob",
              "last_name": "Sagget",
              "email_address": "bob@sagget.com" } }
  */

  if (!users) {
    throw new Error("Unable to retrieve users");
  }

  return assocById(users);

})();

/*
results in the following output

[ { "id": 1,
    "first_name": "Julian",
    "last_name": "Coleman",
    "email_address": "julcol03@gmail.com" },
  { "id": 2,
    "first_name": "Bob",
    "last_name": "Sagget",
    "email_address": "bob@sagget.com" } ]
*/
```

Alternatively, this same thing can be achieved by doing the
following:

```js
API
  .getUser()
  .then(assocByKey("id"))
  .catch(UserNotFoundError, () => 404);
```

---
_Source: https://npm.io/package/@juliancoleman/assoc-by-key · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
