array-key-hash

1.0.2 • Public • Published

array-key-hash Build Status

Get the id keys out of an array and create a hash of the values

Install

$ npm install --save array-key-hash

Usage

var arrayKeyHash = require('array-key-hash')
 
const items = [
  {id: 1, foo: 'a'},
  {id: 2, foo: 'b'}
]
const itemsById = arrayKeyHash(items)
 
console.log(itemsById)
// => {1: {id: 1, foo: 'a'}, 2: {id: 2, foo: 'b'}}

API

arrayKeyHash(array, [idKey]) -> object

array

Required Type: array

idKey

Type: string

The key to index the resulting hash by. Defaults to 'id'.

Returns object

An object with keys representing ids of items in array, and values being the values of those items.

Related / Recommended

License

MIT © Andrew Joslin

Package Sidebar

Install

npm i array-key-hash

Weekly Downloads

1

Version

1.0.2

License

MIT

Last publish

Collaborators

  • andytjoslin