1.1.1 • Published 6 years ago

id-to-key v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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.

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago