array-key-hash

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

Usage no npm install needed!

<script type="module">
  import arrayKeyHash from 'https://cdn.skypack.dev/array-key-hash';
</script>

README

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