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

id-to-key

v1.1.1

Published

Normalize API/JSON data by reducing an array of objects into a new object with their ids as the keys

Downloads

5

Readme

npm version contributions welcome codecov

id-to-key-reducer

This package helps you normalize API or JSON data by reducing an array or object of objects into a new object with their ids as the keys. It is intended to help 'flatten' data for use with libraries such as React and Redux. This reducer works with both id and _id on arrays and objects.

Installation

npm install id-to-key

Usage

const idToKey = require('id-to-key');

or

import idToKey from 'id-to-key';

Example

Use with an Array:

Input:

  const data = [
    {
      name: 'Ben',
      age: 47,
      _id: 1001
    },
    {
      name: 'Aaron',
      age: 27,
      _id: 1002
    },
    {
      name: 'Brendan',
      age: 25,
      _id: 1003
    },
    {
      name: 'Sachin',
      age: 25,
      _id: 1004
    },
    {
      name: 'Ibrahim',
      age: 59,
      _id: 1005
    }
  ];

We want to take the _id property and set it as the key in a new object:

const idToKey = require('id-to-key');

const newData = idToKey(data);

Output:

const newData = {
  1001: {
    name: 'Ben',
    age: 47,
    _id: 1001
  },
  1002: {
    name: 'Aaron',
    age: 27,
    _id: 1002
  },
  1003: {
    name: 'Brendan',
    age: 25,
    _id: 1003
  },
  1004: {
    name: 'Sachin',
    age: 25,
    _id: 1004
  },
  1005: {
    name: 'Ibrahim',
    age: 59,
    _id: 1005
  }
}

Use with an Object:

Input:

  const data = {
    Ben: {
      age: 47,
      _id: 1001
    },
    Aaron: {
      age: 27,
      _id: 1002
    },
    Brendan: {
      age: 25,
      _id: 1003
    },
    Sachin: {
      age: 25,
      _id: 1004
    },
    Ibrahim: {
      age: 59,
      _id: 1005
    }
  };

When using idToKey with an object, you can pass it an 'oldId' argument if you want to set the current key as a property on the object, which should be a STRING. In the example below, 'name' is passed as the second argument, which will give each object a new 'name' property with each name as the value:

const idToKey = require('id-to-key');

const newData = idToKey(data, 'name');

Output:

const newData = {
  1001: {
    age: 47,
    _id: 1001,
    name: 'Ben'
  },
  1002: {
    age: 27,
    _id: 1002,
    name: 'Aaron'
  },
  1003: {
    age: 25,
    _id: 1003,
    name: 'Brendan'
  },
  1004: {
    age: 25,
    _id: 1004,
    name: 'Sachin'
  },
  1005: {
    age: 59,
    _id: 1005,
    name: 'Ibrahim'
  }
}

Coming soon

  • Custom 'id' identifiers; for example 'id-number'

Dependencies

None.