npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objectify-array

v2.1.0

Published

Recursively convert arrays of objects into a single keyed object tree

Downloads

553

Readme

objectify-array Build Status

Recursively convert arrays of objects into a single keyed object tree

Installation

npm install objectify-array --save

or

yarn add objectify-array

Usage

const objectifyArray = require('objectify-array')

var people = [
  {id: 'bob', fullName: 'Bob Bobberson'},
  {id: 'sue', fullName: 'Sue Zinn'},
  {id: 'hal', fullName: 'Hal Itosis'}
]

var peopleMap = objectifyArray(people)

This will return an object, using the id property values as keys:

{
  bob: { id: 'bob', fullName: 'Bob Bobberson' },
  sue: { id: 'sue', fullName: 'Sue Zinn' },
  hal: { id: 'hal', fullName: 'Hal Itosis'}
}

id is the default property name to index by. If you want to use another property, specify it as an optional second argument:

var peopleMap = objectifyArray(people, { by: 'myCustomKeyName' })

You can also specify multiple key names as an array, which are tried in the order you provide them. This is most useful when you use the recursive option:

var todos = [
  {
    id: 10,
    description: 'Learn Things',
    tags: [
      { name: 'a', score: 15 }, { name: 'b', score: 83 }
    ]
  },
  {
    id: 20,
    description: 'Do Things',
    tags: [
      { name: 'x', score: 3 }, { name: 'y', score: 9 }
    ]
  },
]
var todosMap = objectifyArray(todos, { by: ['id', 'name'], recursive: true })

todosMap[10] //=> { id: 10, description: ..., tags: ... }
todosMap[20] //=> { id: 10, description: ..., tags: ... }

todosMap[10].tags.a.score //=> 15
todosMap[10].tags.b.score //=> 83

you can specify options.by to be a function, this function will receive the element and should return the key to be used for this element.

var users = [
    { id: 'u', name: 'Alice', hash: 22 }
  ]
var peopleMap = objectifyArray(people, { by: (x) => `${x.id}_${x.hash}` })

peopleMap.u_22.name //=> Alice

For more extensive usage examples, see test.js

API

The module exports a single function:

objectifyArray(array[, options])

  • array Array - The array of objects to index.
  • options Object (optional) - { by: String | Array<String>, recursive: Boolean }
    • by (default: ['id']) - The key (String) or keys (Array of Strings) to index by.
    • recursive (default: false) - If true, will deeply objectify all arrays in your array of objects.

Tests

npm i && npm t

Dependencies

None

Dev Dependencies

  • standard: JavaScript Standard Style
  • tap-spec: Formatted TAP output like Mocha's spec reporter
  • tape: tap-producing test harness for node and browsers

License

MIT