Socket
Book a DemoInstallSign in
Socket

array-to-obj

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-to-obj

Convert an array of objects into a keyed object

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

array-to-obj Build Status codecov

Convert an array of objects into a keyed object

Install

$ npm install array-to-obj

Usage

Default key is id

const arrayToObj = require('array-to-obj');

const districts = [
  {id: 1, name: 'Richmond'},
  {id: 2, name: 'Cow Hollow, Marina, Pacific Heights'}
];

arrayToObj(districts);

// {
//   1: { id: 1, name: 'Richmond' },
//   2: { id: 2, name: 'Cow Hollow, Marina, Pacific Heights' }
// }

Pass a string as a key selector

const arrayToObj = require('array-to-obj');

const songs = [
  {title: 'Bluebird', artist: 'One Self'},
  {title: 'Sunshine', artist: 'Atmosphere'}
];

arrayToObj(songs, {
  key: 'title'
});

// {
//   Bluebird: { title: 'Bluebird', artist: 'One Self' },
//   Sunshine: { title: 'Sunshine', artist: 'Atmosphere' }
// }

Pass a function as a key selector

const arrayToObj = require('array-to-obj');

const browsers = [
  {engine: 'Blink', name: 'Chrome'},
  {engine: 'Gecko', name: 'Firefox'}
];

arrayToObj(browsers, {
  key: browser => browser.engine.toLowerCase()
});

// {
//   blink: { engine: 'Blink', name: 'Chrome' },
//   gecko: { engine: 'Gecko', name: 'Firefox' }
// }

Try/catch in function selector

const arrayToObj = require('array-to-obj');
const shortid = require('shortid');

const books = [
  {title: 'The Three-Body Problem', meta: {upc: 9781784971571}},
  {title: 'The Dark Forest'}
];

const converted = arrayToObj(books, {
  key: book => {
    try {
      return book.meta.upc;
    } catch (error) {
      return shortid.generate();
    }
  }
});

Pass a function as a key generator

const arrayToObj = require('array-to-obj');
const shortid = require('shortid');

const movies = [
  {released: '2001', title: 'Spirited Away'},
  {released: '2004', title: 'Howl\'s Moving Castle'}
];

arrayToObj(sourceArray, {
  key: () => shortid.generate()
});
// {
//   qy5Tisvbu: { released: '2001', title: 'Spirited Away' },
//   kYzoS40SOn: { released: '2004', title: 'Howl\'s Moving Castle' }
// }

API

arrayToObj(input, [options])

input

Type: array

An array of objects to convert to an object.

options

Type: Object

key

Type: string or function
Default: 'id'

The property to use as a key. The array index is used when the property is undefined.

License

MIT © Chris Vogt

Keywords

array

FAQs

Package last updated on 21 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.